我的代码:
#prints out samenodes f = open('newerfile.txt') mylist = list(f) count = 0 i = 1 while count < 1000: if mylist[i] == mylist[i+12] and mylist [i+3] == mylist [i+14]: print mylist[i] count = count+1 i = i+12我的目的是看elt 1,elt 2.如果elt 1 == elt 13 AND elt 2 == elt 14我想打印elt 1.然后,我想看看elt 13和elt 14.如果elt 2匹配elt 13 + 12和elt 14匹配elt 14 + 12我想打印它。 等等...
我的列表中肯定有部分符合此条件,但程序不返回任何输出。
My code:
#prints out samenodes f = open('newerfile.txt') mylist = list(f) count = 0 i = 1 while count < 1000: if mylist[i] == mylist[i+12] and mylist [i+3] == mylist [i+14]: print mylist[i] count = count+1 i = i+12My intention is to look at elt 1, elt 2. If elt 1 == elt 13 AND elt 2==elt 14 I want to print elt 1. Then, I want to look at elt 13 and elt 14. If elt 2 matches elt 13+12 AND elt 14 matches elt 14+12 I want to print it. ETC...
There are certainly parts of my list that fit this criteria, but the program returns no output.
最满意答案
要在“锁步”中迭代多个列表(技术上,可迭代),您可以使用zip 。 在这种情况下,您希望迭代四个版本的mylist ,偏移mylist和13。
zippedLists = zip(mylist, mylist[12:], mylist[2:], mylist[13:])接下来,您需要第0个,第12个,第24个等元素。 这是通过切片完成的:
slicedList = zippedLists[::12]然后你可以迭代:
for elt1, elt13, elt2, elt14 in slicedList: if elt1 == elt13 and elt2 == elt14: print elt1将它与文件操作放在一起,我们得到
#prints out samenodes f = open('newerfile.txt') mylist = list(f) zippedLists = zip(mylist, mylist[12:], mylist[2:], mylist[13:]) slicedList = zippedLists[::12] for elt1, elt13, elt2, elt14 in slicedList: if elt1 == elt13 and elt2 == elt14: print elt1像这样的代码通常被认为比当前版本更“pythonic”,因为在迭代列表时通常不鼓励使用列表索引。
请注意,如果列表中包含大量元素,则上述代码会创建(并在某些时候销毁)五个额外列表。 因此,如果在itertools使用等效函数,可能会获得更好的内存性能, itertools使用惰性迭代器来防止不必要地复制列表:
from itertools import islice, izip #prints out samenodes f = open('newerfile.txt') mylist = list(f) zippedLists = itertools.izip(mylist, islice(mylist, 12), islice(mylist, 2), islice(mylist, 13)) slicedList = itertools.islice(zippedLists, 0, None, 12) for elt1, elt13, elt2, elt14 in slicedList: if elt1 == elt13 and elt2 == elt14: print elt1在itertools可能有一种方法可以避免将整个文件压入mylist ,但我不确定它是什么 - 我认为这是itertools.tee的用例。
To iterate over multiple lists (technically, iterables) in "lockstep", you can use zip. In this case, you want to iterate over four versions of mylist, offset by 0, 12, 2 and 13.
zippedLists = zip(mylist, mylist[12:], mylist[2:], mylist[13:])Next, you want the 0th, 12th, 24th, etc elements. This is done with slice:
slicedList = zippedLists[::12]Then you can iterate over that:
for elt1, elt13, elt2, elt14 in slicedList: if elt1 == elt13 and elt2 == elt14: print elt1Putting it together with the file operations, we get
#prints out samenodes f = open('newerfile.txt') mylist = list(f) zippedLists = zip(mylist, mylist[12:], mylist[2:], mylist[13:]) slicedList = zippedLists[::12] for elt1, elt13, elt2, elt14 in slicedList: if elt1 == elt13 and elt2 == elt14: print elt1Code like this is generally considered more "pythonic" than your current version, as using list indexes are generally discouraged when you are iterating over the list.
Note that if you've got a huge number of elements in your list the above code creates (and destroys at some point) five extra lists. Therefore, you may get better memory performance if you use the equivalent functions in itertools, which uses lazy iterators to prevent copying lists needlessly:
from itertools import islice, izip #prints out samenodes f = open('newerfile.txt') mylist = list(f) zippedLists = itertools.izip(mylist, islice(mylist, 12), islice(mylist, 2), islice(mylist, 13)) slicedList = itertools.islice(zippedLists, 0, None, 12) for elt1, elt13, elt2, elt14 in slicedList: if elt1 == elt13 and elt2 == elt14: print elt1There's probably a way in itertools to avoid slurping the entire file into mylist, but I'm not sure I remember what it is - I think that is the use case for itertools.tee.
更多推荐
发布评论