大佬教程收集整理的这篇文章主要介绍了从 2 个列表中找到 anagram 对并创建 anagram 的元组列表,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
说我有两个列表
List_1 = [ 'Tar','Arc','Elbow','State','cider','Dusty','Night','Inch','Brag','Cat','Bored','Save','Angel','bla','Stressed','Dormitory','School master','Awesoame','Conversation','Listen','Astronomer','The eyes','A gentleman','Funeral','The Morse Code','Eleven plus two','Slot machines','Fourth of July','Jim Morrison','damon Albarn','George Bush','Clint Eastwood','Ronald Reagan','Elvis','Madonna Louise Ciccone','bart','Paris','San DIEgo','Denver','Las Vegas','Statue of liberty']
和
LisT_B = ['CrIEd','He BUGs Gore','They see','lives','Joyful Fourth','The classroom','Diagnose','Silent','Taste','Car','Act','Nerved','Thing','A darn long era','Brat','Twelve plus one','Elegant man','Below','Robed','study','Voices rant on','Chin','Here come dots','Real fun','Pairs','Desserts','Moon starer','Dan Abnormal','old West action','Built to stay free','One cool dance musician','Dirty room','Grab','Salvages','Cash lost in me',"Mr. Mojo Risin'",'Glean','Rat','Vase']
我正在寻找的是在 LisT_B 中找到 List_A 的字谜对。创建一个字谜元组列表。 对于一个列表,我可以执行以下操作并生成元组列表,但是,对于两个列表,我需要一些帮助。在此先感谢您的帮助!
我为一份清单所做的尝试,
from collections import defaultDict
anagrams = defaultDict(List)
for w in List_A:
anagrams[tuple(sorted(w))].append(w)
您可以使用嵌套的 for 循环,第一个列表为外部,第二个为内部(另外,使用 str.lower
使其不区分大小写):
anagram_pairs = [] # (w_1 from list_A,w_2 from lisT_B)
for w_1 in list_A:
for w_2 in lisT_B:
if sorted(w_1.lower()) == sorted(w_2.lower()):
anagram_pairs.append((w_1,w_2))
print(anagram_pairs)
输出:
[('Tar','Rat'),('Arc','Car'),('Elbow','Below'),('State','Taste'),('Cider','Cried'),('Dusty','study'),('Night','Thing'),('Inch','Chin'),('Brag','Grab'),('Cat','Act'),('Bored','Robed'),('Save','Vase'),('Angel','Glean'),('Stressed','Desserts'),('School master','The classroom'),('Listen','Silent'),('The eyes','They see'),('A gentleman','Elegant man'),('The Morse Code','Here come dots'),('Eleven plus two','Twelve plus one'),('Damon Albarn','Dan Abnormal'),('Elvis','Lives'),('Bart','Brat'),('Paris','Pairs'),('Denver','Nerved')]
,
您与当前的尝试非常接近。您需要做的就是在 lisT_B
上重复相同的过程:
from collections import defaultDict
anagrams = defaultDict(list)
list_A = [ 'Tar','Arc','Elbow','State','Cider','Dusty','Night','Inch','Brag','Cat','Bored','Save','Angel','bla','Stressed','Dormitory','School master','Awesoame','Conversation','Listen','Astronomer','The eyes','A gentleman','Funeral','The Morse Code','Eleven plus two','Slot machines','Fourth of July','Jim Morrison','Damon Albarn','George Bush','Clint Eastwood','Ronald Reagan','Elvis','Madonna Louise Ciccone','Bart','Paris','San Diego','Denver','Las Vegas','Statue of Liberty']
lisT_B = ['Cried','He bugs Gore','They see','Lives','Joyful Fourth','The classroom','Diagnose','Silent','Taste','Car','Act','Nerved','Thing','A darn long era','Brat','Twelve plus one','Elegant man','Below','Robed','study','Voices rant on','Chin','Here come dots','Real fun','Pairs','Desserts','Moon starer','Dan Abnormal','Old West action','Built to stay free','One cool dance musician','Dirty room','Grab','Salvages','Cash lost in me',"Mr. Mojo Risin'",'Glean','Rat','Vase']
for w in list_A:
anagrams[tuple(sorted(w))].append(w)
for w in lisT_B:
anagrams[tuple(sorted(w))].append(w)
result = [b for b in anagrams.values() if len(b) > 1]
输出:
[['Cider','Cried'],['The eyes','They see'],['Damon Albarn','Dan Abnormal'],['Bart','Brat'],['Paris','Pairs']]
,
另一种使用字典的解决方案:
out = {}
for word in list_A:
out.setdefault(tuple(sorted(word.lower())),[]).append(word)
for word in lisT_B:
word_s = tuple(sorted(word.lower()))
if word_s in out:
out[word_s].append(word)
print(list(tuple(v) for v in out.values() if len(v) > 1))
打印:
[
("Tar","Rat"),("Arc","Car"),("Elbow","Below"),("State","Taste"),("Cider","Cried"),("Dusty","study"),("Night","Thing"),("Inch","Chin"),("Brag","Grab"),("Cat","Act"),("Bored","Robed"),("Save","Vase"),("Angel","Glean"),("Stressed","Desserts"),("School master","The classroom"),("Listen","Silent"),("The eyes","They see"),("A gentleman","Elegant man"),("The Morse Code","Here come dots"),("Eleven plus two","Twelve plus one"),("Damon Albarn","Dan Abnormal"),("Elvis","Lives"),("Bart","Brat"),("Paris","Pairs"),("Denver","Nerved"),]
以上是大佬教程为你收集整理的从 2 个列表中找到 anagram 对并创建 anagram 的元组列表全部内容,希望文章能够帮你解决从 2 个列表中找到 anagram 对并创建 anagram 的元组列表所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。