[英]How can I make this looping Python code more efficient using list comprehension?
I am having trouble converting the logic of the code shown below to use Python comprehension.我无法将下面显示的代码逻辑转换为使用 Python 理解。 Can anyone help me out?
谁能帮我吗?
Premise前提
lst1
is a list of matching word pairs, lst2
is a list of matching word sextets. lst1
是匹配词对的列表, lst2
是匹配词六重奏的列表。 I am trying to "stitch" together any two of the entries in lst2
to form a loop of twelve words using the word pairs from lst1
by matching the beginning and ending words in lst2
entries with entries lst1
.我试图将
lst2
中的任意两个条目“缝合”在一起,以使用lst1
中的单词对形成十二个单词的循环,方法是将lst2
条目中的开头和结尾单词与条目lst1
。
Inputs输入
lst1
and lst2
are 2D lists formatted as follows (with some sample data): lst1
和lst2
是 2D 列表,格式如下(带有一些示例数据):
lst1 = [['RUNS', 'SHORT'],
['HIGH', 'HORSE'],
['TRUE', 'FALSE'],
['KEEP', 'HOUSE']
]
lst2 = [['SHORT', 'FILM', 'PROP', 'PLANE', 'RIDE', 'HIGH'],
['FRONT', 'DOOR', 'NAIL', 'SALON', 'DECK', 'HAND'],
['HORSE', 'BACK', 'FLIP', 'PHONE', 'HOME', 'RUNS']
]
My Code我的代码
The decidedly un-Pythonic code I have come up with is:我想出的绝对非 Pythonic 代码是:
ans = []
for i in lst1:
for j in lst2:
for k in lst2:
if j[-1] == i[0] and k[0] == i[1]:
item = j + k
for m in lst1:
if m[1] == j[0] and m[0] == k[-1]:
ans.append(item)
print(ans)
# becasue by definition j[0] = item[0] and k[-1] = item[-1],
# the last if statement can also be written as:
# if m[1] == item[0] and m[0] == item[-1]:
Output Output
This code run on the above data produces the following output:此代码在上述数据上运行产生以下 output:
[['HORSE', 'BACK', 'FLIP', 'PHONE', 'HOME', 'RUNS',
'SHORT', 'FILM', 'PROP', 'PLANE', 'RIDE', 'HIGH'],
['SHORT', 'FILM', 'PROP', 'PLANE', 'RIDE', 'HIGH',
'HORSE', 'BACK', 'FLIP', 'PHONE', 'HOME', 'RUNS']
]
Note that [ans[i][-1], ans[i][0]]
is in lst2
for each of the entries in ans
, as required to "loop" around to the beginning again to form a circle of words.请注意,对于 ans 中的每个条目, [
ans
[ans[i][-1], ans[i][0]]
都在lst2
中,因为需要再次“循环”到开头以形成一个单词圈。 Because of the circular nature of the word loop, the two entries in ans
are the same word loop and are, essentially, duplicate entries.由于单词循环的循环特性,
ans
中的两个条目是同一个单词循环,并且本质上是重复的条目。 I would need to go back and clean up this duplication with additional code that I haven't figured out yet.我需要返回 go 并使用我还没有弄清楚的其他代码清理这个重复。
(Side note: These nested for
loops runs extremely slow on lists with tens of thousands of entries. Any ideas on how I can speed things up with different logic? The word lists may have duplicate entries that need to be preserved so dict()
or set()
won't work.) (旁注:这些嵌套
for
循环在具有数万个条目的列表上运行非常慢。关于如何使用不同的逻辑加快速度的任何想法?单词列表可能有重复的条目需要保留,因此dict()
或set()
不起作用。)
Thank you in advance.先感谢您。 I and a bit of a noob to Python and can use all the help I can get.
我和 Python 有点菜鸟,可以使用我能得到的所有帮助。
alkemyst炼金术士
This is what I was looking for:这就是我一直在寻找的:
ans = [ j + k for i in corner
for w in corner
for j in chain
for k in chain
if j[-1] == i[0] and k[0] == i[1]
if w[1] == j[0] and w[0] == k[-1]
]
print(ans)
It is marginally faster.它稍微快一点。 Thank you for all your comments.
感谢您的所有评论。
alkemyst炼金术士
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.