开发者

Python limit of the * operator and zip() function

开发者 https://www.devze.com 2023-02-14 08:46 出处:网络
I have a Python code similar to this one: for lines in zip(*files): # do something where files is a list of files, each file is a list of lines and each line is a list of strings. Therefore, the co

I have a Python code similar to this one:

for lines in zip(*files):
   # do something

where files is a list of files, each file is a list of lines and each line is a list of strings. Therefore, the code above should first unpack the list files and then apply the function zip(), returning a tuple with the first line of each file. The problem is that this works just fine, if the length of the list of files is 30 (for example). However, if the lenght is bigger, for instance, 120, the code inside the loop doesn't get executed even once.

The conclusion is that either the zip() function is returning an empty list or the * operator is not doing its job. Either way, my question is if there is a limit in the arguments that zip() can handle (or * can unpack) or it is somehow li开发者_JAVA技巧mited by the amount of memory that my computer has, since I haven't been able to find anything in Python's documentation.

PS: I'm running Python 2.4


If one of the files is empty, zip will return an empty list. As of Python 2.6 you can use itertools.izip_longest to handle that. On older versions, you can use map(None, *files), courtesy of @Sven Marnach.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号