1.如何实现可迭代对象和迭代器对象(1)¶ In [1]: # 列表和字符串都是可迭代对象 l = [1,2,3,4] In [2]: s = 'abcde' In [3]: for x in l:print(x) 1 2 3 4 In [4]: for x in s:print(x) a b c d e In [5]: iter(l) Out[5]: <list_iterator at 0x7fde5571c208> In [6]: iter(s) Out[6]: <str…
1.如何在列表, 字典, 集合中根据条件筛选数据¶ In [1]: from random import randint In [2]: data = [randint(-10,10) for _ in range(10)] In [3]: data Out[3]: [4, 4, -5, 6, 7, 10, 5, -7, -6, -9] In [4]: # 筛选出列表中大于0的元素 # 使用filter函数 list(filter(lambda x:x>=0,data)) Out[4]: […