标签:列表 推导式 使用 ict eve code 集合推导式 排错 bug
l1 = [i for i in range(1,11)]
l2 = [i ** 2 for i in range(100)]
l3 = [f'python{i}' for i in range(50)]
筛选模式:
l1 = [i for i in range(1,31) if i % 3 == 0]
嵌套循环:
names = [['tom','billy','jefferson'],['wesley','steven','jennifer']]
e = [ name for i in names for name in i if name.count('e') == 2]
e = ['jefferson','wesley','steven','jennifer']
循环模式:
g = (i for i in range(1,11))
print(next(g)) #1
print(next(g)) #2
筛选模式
g = (i for i in range(50) if ...)
print(next(g)) #1
print(next(g)) #2
列表推导式-缺点:
? 有毒,不要太着迷
? 超过三层循环才能构建成功的,不建议使用
? 无法使用Debug 进行排错
列表推导式-优点:
? 一行结构,简单
? zb (23..)
porker = [ i for i in range(2,11)] + list('JQKA')
# [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
写法上 [] ()
iterable iterator
list1 = ['jay','jj','meet']
list2 = ['周杰伦','林俊杰','panda']
mdict = {list2[i]: list[1] for i in range(len(list1))}
#{'周杰伦': 'jay', '林俊杰': 'jj', 'panda': 'meet'}
{i for i in range(1,11)}
标签:列表 推导式 使用 ict eve code 集合推导式 排错 bug
原文地址:https://www.cnblogs.com/pandaa/p/12038430.html