从 2 个列表中查找变位词对并创建变位词元组列表

Find the anagram pairs of from 2 lists and create a list of tuples of the anagrams

假设我有两个列表

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')]

另一个使用字典的解决方案:

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"),
]

您目前的尝试已经非常接近了。您需要做的就是在 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']]