列表推导式

一、列表推导式

example:20以内被3整除的数据

lst = [i for i in range(20) if i%3 is 0]
print(lst)
[0, 3, 6, 9, 12, 15, 18]

example:取含有两个'e‘的单词

names = [['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
         ['January', 'February', 'March', 'April', 'May', 'June', 'July','August','September','October','November','December']]

print([name for lst in names for name in lst if name.count('e') >= 2])
['Wednesday', 'September', 'November', 'December']

二、集合推导式

example

lst = { x+1 for x in range(10)}
print(lst)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

  

posted @ 2019-03-27 13:53  ☞Python☞  阅读(158)  评论(0编辑  收藏  举报