我有这两个实现来计算有限生成器的长度,同时保留数据以供进一步处理:
def count_generator1(generator):
'''- build a list with the generator data
- get the length of the data
- return both the length and the original data (in a list)
WARNING: the memory use is unbounded, and infinite generators will block this'''
l = list(generator)
return len(l), l
def count_generator2(generator):
'''- get two generators from the original generator
- get the length of the data from one of them
- return both the length and the original data, as returned …Run Code Online (Sandbox Code Playgroud) 我正在尝试压缩两个长度相同的列表,但我总是收到错误“zip object at 0x0000000002A81548”而不是压缩列表。
filename = input("Which file do you want to open?: \n")
file = open("C:/"+ filename,'r')
movielist = []
moviename = []
moviedate = []
for line in file:
line.strip()
name = re.search('name:(.*)',line)
date = re.search('date:(.*)',line)
if name:
titel = name.group(1)
moviename.append(titel)
if date:
datum = date.group(1)
moviedate.append(datum)
print("Name of the list: ", moviename.pop(0))
movielist= zip(moviename,moviedate)
print(movielist)
print("Number of movies: " , len(moviename))
Run Code Online (Sandbox Code Playgroud)