思路:
列表解析:[for x in data if x >= 0]
filter函数:filter(lambda x:x>=0 ,data)
字典解析:{k,v for k,v in d.items() if v > 90}
集合:{x for x in s if x % 3 == 0}
代码:
In [7]: from random import randint
In [8]: l = [ randint(-10,10) for _ in range(10) ]
In [9]: l
Out[9]: [-9, -7, 4, -9, 3, 10, 3, 10, -2, 4]
In [10]: [x for x in l if x >= 0]
Out[10]: [4, 3, 10, 3, 10, 4]
In [11]: g = filter(lambda x: x>=0,l)
In [12]: g
Out[12]: <filter at 0x7fa6af0967b8>
In [13]: next(g)
Out[13]: 4
In [14]: next(g)
Out[14]: 3
In [15]: next(g)
Out[15]: 10
In [16]: next(g)
Out[16]: 3
In [17]: next(g)
Out[17]: 10
In [18]:
In [18]: next(g)
Out[18]: 4
In [19]: next(g)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-19-e734f8aca5ac> in <module>
----> 1 next(g)
StopIteration:
In [20]: list(g)
Out[20]: []
In [21]: g = filter(lambda x: x>=0,l)
In [22]: list(g)
Out[22]: [4, 3, 10, 3, 10, 4]
In [23]: {'Student%d' % i: randint(50,100) for i in range(1,21)}
Out[23]:
{'Student1': 70,
'Student2': 67,
'Student3': 90,
'Student4': 81,
'Student5': 86,
'Student6': 50,
'Student7': 67,
'Student8': 86,
'Student9': 73,
'Student10': 95,
'Student11': 96,
'Student12': 67,
'Student13': 68,
'Student14': 58,
'Student15': 92,
'Student16': 67,
'Student17': 81,
'Student18': 54,
'Student19': 100,
'Student20': 68}
In [24]: d = {'Student%d' % i: randint(50,100) for i in range(1,21)}
In [25]: d
Out[25]:
{'Student1': 69,
'Student2': 71,
'Student3': 75,
'Student4': 76,
'Student5': 74,
'Student6': 79,
'Student7': 99,
'Student8': 95,
'Student9': 98,
'Student10': 54,
'Student11': 60,
'Student12': 67,
'Student13': 59,
'Student14': 97,
'Student15': 79,
'Student16': 51,
'Student17': 92,
'Student18': 84,
'Student19': 78,
'Student20': 64}
In [26]: {k:v for k,v in d.items() if v >= 90}
Out[26]:
{'Student7': 99,
'Student8': 95,
'Student9': 98,
'Student14': 97,
'Student17': 92}
In [27]: d.items()
Out[27]: dict_items([('Student1', 69), ('Student2', 71), ('Student3', 75), ('Student4', 76), ('Student5', 74), ('Student6', 79), ('Student7', 99), ('Student8', 95), ('Student9', 98), ('Student10', 54), ('Student11', 60), ('Student12', 67), ('Student13', 59), ('Student14', 97), ('Student15', 79), ('Student16', 51), ('Student17', 92), ('Student18', 84), ('Student19', 78), ('Student20', 64)])
In [28]: g = filter(lambda item:item[1] >= 90,d.items())
In [29]: g
Out[29]: <filter at 0x7fa6aeec4630>
In [30]: list(g)
Out[30]:
[('Student7', 99),
('Student8', 95),
('Student9', 98),
('Student14', 97),
('Student17', 92)]
In [31]: g = filter(lambda item:item[1] >= 90,d.items())
In [32]: dict(g)
Out[32]:
{'Student7': 99,
'Student8': 95,
'Student9': 98,
'Student14': 97,
'Student17': 92}
In [33]: {randint(0,20) for _ in range(20)}
Out[33]: {5, 6, 7, 8, 10, 11, 12, 13, 14, 17, 18, 19, 20}
In [34]: s = {randint(0,20) for _ in range(20)}
In [35]: s
Out[35]: {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16}
In [36]: { x for x in s if x % 3 == 0 }
Out[36]: {6, 9, 12}
In [37]: