Python zip() 两个列表 [英] Python zip() two lists

查看:48
本文介绍了Python zip() 两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试压缩两个长度相同的列表,但总是出现错误在 0x0000000002A81548 处压缩对象"而不是压缩列表.

I'm trying to zip two lists with the same length, but I always get the error "zip object at 0x0000000002A81548" instead of a zipped list.

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))

推荐答案

movielist= list(zip(moviename,moviedate))

在 Python 2 中 zip 返回一个列表,上面不需要.在 Python 3 中,它返回一个特殊的惰性 zip 对象,类似于生成器或 itertools.izip,您需要将其具体化以使用 len.map 也是如此.

In Python 2 zip returns a list and the above is not needed. In Python 3 it returns a special lazy zip object, similar to a generator or itertools.izip, which you need to make concrete to use len. The same is true for map.

这篇关于Python zip() 两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆