循环以匹配列表的部分(Loop to Match Parts of List)

编程入门 行业动态 更新时间:2024-10-27 17:24:08
循环以匹配列表的部分(Loop to Match Parts of List)

我的代码:

#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+12

My 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 elt1

Putting 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 elt1

Code 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 elt1

There'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.

更多推荐

本文发布于:2023-08-05 16:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1436705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Loop   Match   Parts   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!