1、排序
b = ['see', 'seek', 'se', 'sleep', 'six']
b.sort(key=len)
print("长度排序",b)
seq = [7, 2, 3, 7, 5, 6, 0, 1]
# c=sorted(seq)
c01=sorted(b[1])
print("字符排序",c01)
seq.sort(reverse=True)
print("正反排序",seq)
c=seq[:2]
print("切前两个",c)
c1=seq[::2]
print("步长2切",c1)
# c2=seq[::-1]
c2=list(reversed(seq))
print("反向排序",c2)
2、合成zip pop set....
aaa=("aoo","app","acc")
map={}
for i,v in enumerate(aaa):
map[i]=v
print("合成字典",map)
seq1 = ("aoo","app","acc")
seq2 = ['one', 'two', 'three']
zipped = zip(seq1, seq2)
print("zip合并",list(zipped))
seq3=(1,2,3)
for i, (a, b ,c)in enumerate(zip(seq1,seq2,seq3)):
print("zip分组",{'{0}: {1}, {2}, {3}'.format(i, a, b,c)})
pitchers =[('aoo', 'one'), ('app', 'two'), ('acc', 'three')]
firstname,lastname=zip(*pitchers)
print("*zip解压列表",firstname,lastname)
mapp={}
for i, v in enumerate(firstname+lastname):
mapp[v]=i
print("合成字典",mapp)
d1={'a': 'acc', 'b': [1, 2, 3, 4], 7: 'app'}
d1["111"]=11111
d1.update({"222":22222,"333":33333})
print("字典中添加键值对",d1)
ret=d1.pop("111")
print("pop取出“111”的值再删除",ret,d1)
print("取出键值对",list(d1.keys()),d1.values())
mapping = {}
for key, value in zip(d1.keys(),d1.values()):
mapping[key] = value
print("键值对合并",mapping)
mapping = dict(zip(range(5), reversed(range(5))))
print("两元祖合并",mapping)
words = ['app', 'bat', 'bar', 'acc', 'bit']
by_letter = {}
for word in words:
letter=word[0]
if letter not in by_letter:
by_letter[letter]=[word]
else:
by_letter[letter].append(word)
print("首字母提取字典",by_letter)
d={}
d[tuple([1,2,3])]=4
print("键必须是元祖字符整形",d)
a=set([2, 2, 2, 1, 3, 3])
print("set集合去重",a)
3、交集并集
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
# c=a.union(b)
c=a | b
# c1=a.intersection(b)
c1=a & b
print("并集{}:",c,"交集{}:",c1)
d=a.copy()
d|=b
d&=b
print(d)
dd={1,2,3}.issubset(d)
dd1={3,4,5}.issubset(d)
dd2={1,2,3}=={3,2,1}
print("判断子集",dd,dd1,dd2)
4、for in
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
ii=[i.upper() for i in strings if len(i)>2]
print("for/if用法[]:",ii)
5、元组拆分
some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
flaater=[i for ii in some_tuples for i in ii ]
print(flaater)
flatter=[[x for x in tup] for tup in some_tuples]
print("多元组拆分",flatter)
6、返回多值
def func():
a=1
b=2
c=3
return {"a":a,"b":b,"c":c}
return_value=func()
print("返回多值字典{}:",return_value)
7、lambda
def apply_to_list(some_list, f):
return [f(x) for x in some_list]
ints = [4, 0, 1, 5, 6]
aa=apply_to_list(ints, lambda x: x * 2)
print("函数作为参数",aa)
strings = ['aoo', 'card', 'bar', 'aaaa', 'abab']
strings.sort(key=lambda x: len(set(list(x))))
print("传入lambda到sort",strings)
8、生成器(一般对列表对象或文件中的行迭代)
def squares(n=10):
print('1 to {0}'.format(n ** 2))
for i in range(1, n + 1):
yield i ** 2
gen = squares()
print("生成器yield",gen)
gen2=[]
for gen1 in squares():
gen2.append(gen1)
print("生成器返回值需迭代请求返回",gen2)
gen = (x ** 2 for x in range(10))
for gen1 in gen:
gen2.append(gen1)
print("迭代器返回值需迭代请求返回",gen2)
summ=sum(x ** 2 for x in range(100))
print(summ)
9、异常
def att(x):
try:
return float(x)
except (TypeError, ValueError):###try失败,排除类型错误值错误后返回
return x
a12=att(([1],2.,"abc"))
print("抛出异常",a12)
try:
# write_to_file()
# except:
# print("failed")
# else:
# print("secceed") ### try成功执行else”succeed“,try失败执行except,最后必须执行finally
# finally:
# f.close()
10、正则表达式
states = [' Acc ', 'Gee!', 'Gee', 'geely', 'FlOr', 'saic car##', 'We vv?']
import re
def clean_strings(string):
result=[]
for value in string:
value=value.strip()
value=re.sub("[!#?]","+",value)
value=value.title()
result.append(value)
return(result)
clc_str=clean_strings(states)
print("re清理数据",clc_str)