<list_iterator object at 0x794782a8e230>
<tuple_iterator object at 0x794782a8c8e0>
<dict_keyiterator object at 0x794782ab3560>
1
1
a
2
2
b
3
3
c
4
4
d
5
5
e
Generator
import os
import psutil
defshow_memory_info(hint):
pid = os.getpid()
p = psutil.Process(pid)
info = p.memory_full_info()
memory = info.uss /1024./1024print(f"[{time.ctime()}] {hint} memory used: {memory:.2f} MB")
#test_iterator,test_generatorimport time
deftest_iterator():
show_memory_info("initing iterator")
list_1 =[i for i inrange(100000000)]
show_memory_info("after iterator initiated")print(sum(list_1))
show_memory_info("after sum called")deftest_generator():
show_memory_info("initing generator")
list_2 =(i for i inrange(100000000))
show_memory_info("after generator initiated")print(sum(list_2))
show_memory_info("after sum called")%time test_iterator()%time test_generator()
[Tue Aug 27 09:24:13 2024] initing iterator memory used: 190.07 MB
[Tue Aug 27 09:24:23 2024] after iterator initiated memory used: 3984.98 MB
4999999950000000
[Tue Aug 27 09:24:24 2024] after sum called memory used: 3984.98 MB
CPU times: user 6.02 s, sys: 6.4 s, total: 12.4 s
Wall time: 12.5 s
[Tue Aug 27 09:24:26 2024] initing generator memory used: 211.91 MB
[Tue Aug 27 09:24:26 2024] after generator initiated memory used: 211.91 MB
4999999950000000
[Tue Aug 27 09:24:35 2024] after sum called memory used: 209.95 MB
CPU times: user 7.75 s, sys: 8.37 ms, total: 7.76 s
Wall time: 9.38 s
它还真的是一个Generator
list_2 =(i for i inrange(100000000))type(list_2)
generator
生成器 yield
defgenerator(k):
i =1whileTrue:yield i ** k
i +=1
gen_1 = generator(1)
gen_3 = generator(3)print(gen_1)print(gen_3)defget_sum(n):
sum_1, sum_3 =0,0for i inrange(n):
next_1 =next(gen_1)
next_3 =next(gen_3)print('next_1 = {}, next_3 = {}'.format(next_1, next_3))
sum_1 += next_1
sum_3 += next_3
print(sum_1 * sum_1, sum_3)
get_sum(8)
defindex_normal(L, target):
result =[]for i, num inenumerate(L):if num == target:
result.append(i)return result
print(index_normal([1,6,2,4,5,2,8,6,3,2],2))
[2, 5, 9]
defindex_generator(L, target):for i, num inenumerate(L):if num == target:yield i
print(list(index_generator([1,6,2,4,5,2,8,6,3,2],2)))
[2, 5, 9]
## 算法题defis_subsequence(a, b):
b =iter(b)returnall(i in b for i in a)print(is_subsequence([1,3,5],[1,2,3,4,5]))print(is_subsequence([1,4,3],[1,2,3,4,5]))
True
False
defis_subsequence(a, b):
b =iter(b)print(b)
gen =(i for i in a)print(gen)for i in gen:print(i)# (i in b ) 判断i是否在b中
gen =((i in b)for i in a)print(gen)for i in gen:print(i)returnall(((i in b)for i in a))print(is_subsequence([1,3,5],[1,2,3,4,5]))#print(is_subsequence([1, 4, 3], [1, 2, 3, 4, 5]))
<list_iterator object at 0x7878b7331030>
<generator object is_subsequence.<locals>.<genexpr> at 0x7878b733c4a0>
1
3
5
<generator object is_subsequence.<locals>.<genexpr> at 0x7878b733c510>
True
True
True
False