列表解析
from random import randint
data =[randint(-10,10) for _ in range(10)]
1.[x for x in data if x >0] 列表解析
2.filter(lambda x:x>0,data)
3.res =[]
for x in data:
if x>0:
res.append(x)
1速度最快,2其次,3不好
字典解析
from random import randint
d ={x:randint(60,100) for x in range(1,21)}
{k:v for k,v in d.items() if v >90} 字典解析
集合解析
s= [randint(-10,10) for _ in range(10)]
t = set(s)
{x for x in t if x%3 ==0}
from collections import namedtuple
student = namedtuple('Student',['name','age','sex','emil'])
a = student('jim',16,'man','ssss@163.com')
print(a.name)