python3实用编程技巧——数据结构

1. 如何拆分包含多种分隔符的字符串

      例: >>> s='ab;cd|efg|hi,jkl|mn opq;rst,uvw xyz'

      ********************str.split 缺点: 每次只能处理一种分隔符, 推荐:只有一个分隔符进行分割的时候使用***********************

  extend  sum, reduce
>>> s.split(';')
['ab', 'cd|efg|hi,jkl|mn opq', 'rst,uvw xyz']
>>> [ss.split("|") for ss in s.split(";")]
[['ab'], ['cd', 'efg', 'hi,jkl', 'mn opq'], ['rst,uvw xyz']]
>>>
>>>
>>> lambda ss:ss.split("|"),s.split(";")
(<function <lambda> at 0x000002DB4AA11CA8>, ['ab', 'cd|efg|hi,jkl|mn opq', 'rst,uvw xyz'])
>>>
>>> map(lambda ss:ss.split("|"),s.split(";"))
<map object at 0x000002DB4AA95748>
>>> list(map(lambda ss:ss.split("|"),s.split(";")))
[['ab'], ['cd', 'efg', 'hi,jkl', 'mn opq'], ['rst,uvw xyz']]
>>>

 >>> t=[]
>>> list(map(t.extend,ss.split("|") for ss in s.split(";")))
SyntaxError: invalid character in identifier
>>> list(map(t.extend,[ss.split("|") for ss in s.split(";")]))
[None, None, None]
>>> t
['ab', 'cd', 'efg', 'hi,jkl', 'mn opq', 'rst,uvw xyz']

 
s='ab;cd|efg|hi,jkl|mn opq;rst,uvw xyz'
函数:
def my_split(s,seps):
    res=[s]
    for sep in seps:
        t=[]
        list(map(lambda ss:t.extend(ss.split(sep)),res))
        res = t
    return res
print (my_split(s,",;| "))

s='ab;cd|efg|hi,jklmn opq;rst,uvw xyz'
k =sum([ss.split("|") for ss in s.split(";")][])
print (k)

>>>['ab', 'cd', 'efg', 'hi,jkl', 'mn opq', 'rst,uvw xyz']

from functools import reduce
my_split = lambda s, seqs: reduce(lambda l,sep:sum(map(lambda ss:ss.split(sep),l), []), seqs, [s])
print (kk)

                         python 直接将list 整体转化-----------map():

                         >>> results = ['1', '2', '3'] 转化为下面这个样子[1, 2, 3]

                        python2  

                       >>>map(int, results)

                       python3:

                       >>>list(map(int, results))

                        [1, 2, 3]

      ********************************正则表达式:re.split****************************

                   推荐: 多个分隔符的时候用,只有一个分割符的话,考虑到性能,建议使用str.split, 

          >>>import re
                    >>>print (re.split("[;,| ]+",s))

                    >>>['ab', 'cd', 'efg', 'hi', 'jkl', 'mn', 'opq', 'rst', 'uvw', 'xyz']

 2. 如何在列表,字典,集合中根据条件筛选数据

          例: 

  基础方法  推荐使用

list:

随机生成一个list:

from ramdom import ramdint 

[randint(-50, 50) for _ in range(10)]

#[-50, -10, 40, 19, 38, 42, 4, 17, -34, -37]

例:

res = []
for l in test_list:
    if l>=0:
        res.append(l)
print (res)
 
列表解析和filter函数

print ([x for x in test_list if x>=0])
 

filter(lambda x:x>0,test_list)
注意在python3中:filter返回的是一个生成器对象,在python2里面可以直接返回一个列表
要直接获取最后的list结果的话,需要:
list(filter(lambda x:x>0,test_list))

字典:

随机生成一个字典:

{'student%d' % i:randint(50,100) for i in range(1,21)}

#{'student1': 100, 'student2': 97, 'student3': 67, 'student4': 73, 'student5': 67, 'student6': 54, 'student7': 80, 'student8': 83, 'student9': 69, 'student10': 63, 'student11': 69, 'student12': 53, 'student13': 77, 'student14': 75, 'student15': 71, 'student16': 57, 'student17': 61, 'student18': 77, 'student19': 93, 'student20': 93}

 

字典解析:

  {k:v for k,v in d.items() if v>90}

filter:
dict(filter(lambda item:item[1]>=90, g.items()))

集合:

{randint(0, 30) for _ in range(20)}

 

集合解析

    {x for x in s if x%3==0}

       

 3. 如何给元组里面的元素命名,提高程序的可读性

          

                       

原文地址:https://www.cnblogs.com/ting152/p/12509807.html