相关疑难解决方法(0)

在Python中获取迭代器中的元素数量

有没有一种有效的方法来了解Python中迭代器中有多少元素,一般来说,没有遍历每个元素并进行计数?

python iterator

117
推荐指数
10
解决办法
10万
查看次数

有限发电机的长度

我有这两个实现来计算有限生成器的长度,同时保留数据以供进一步处理:

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)

python generator

7
推荐指数
1
解决办法
2105
查看次数

Python zip() 两个列表

我正在尝试压缩两个长度相同的列表,但我总是收到错误“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)

python zip python-3.x

3
推荐指数
1
解决办法
7579
查看次数

标签 统计

python ×3

generator ×1

iterator ×1

python-3.x ×1

zip ×1