通过组合 Python 中的两个列表生成新值
Generating new values by combining two lists in Python
我有一系列我标记化的短语。然后,我找到了每个单词的同义词,并将它们保存在以单词(即令牌)为键、同义词列表为值的字典中。我的目标是通过用同义词替换每个单词并创建新短语来生成新短语。
例如,我们有一个包含 3 个标记的短语。第一个 token (limited) 有 18 个同义词,第二个 token (social) 有 4 个同义词,最后一个 token (support) 有 16 个同义词。所以理论上我们可以通过将所有 3 个列表组合在一起得到 18 * 4 * 16 = 1,152
个新短语。
phrases = ['limited', 'social', 'support']
dictionary = {
'limited': ['express', 'limited', 'restrict', 'restrain', 'trammel', 'limit', 'bound', 'confine', 'throttle', 'circumscribe', 'specify', 'set', 'determine', 'define', 'fix', 'circumscribed', 'modified', 'special'],
'social': ['sociable', 'social', 'mixer', 'societal'],
'support': ['support', 'reinforcement', 'reenforcement', 'documentation', 'keep', 'livelihood', 'living', 'bread_and_butter', 'sustenance', 'supporting', 'accompaniment', 'musical_accompaniment', 'backup', 'financial_support', 'funding', 'backing']
}
new_phrases = [['express', 'sociable', 'support'], ['express', 'social', 'support'], ['express', 'mixer', 'support'], ['express', 'societal', 'support'], ..., [...]]
我的尝试是遍历每个列表中的项目,但我很难概念化如何将这 3 个列表组合在一起以生成类似于 new_phrases
的内容,如上面的代码块所示。
for word in phrases:
print("\nthe word is:", word)
print("list of synonyms is:", dictionary[word])
print("the list has", len(dictionary[word]), "elements")
for syn in dictionary[word]:
print("a synonmy is:", syn)
import itertools
new_phrases = list(itertools.product(*dictionary.values()))
>>> new_phrases
[('express', 'sociable', 'support'),
('express', 'sociable', 'reinforcement'),
('express', 'sociable', 'reenforcement'),
('express', 'sociable', 'documentation'),
('express', 'sociable', 'keep'),
('express', 'sociable', 'livelihood'),
('express', 'sociable', 'living'),
('express', 'sociable', 'bread_and_butter'),
('express', 'sociable', 'sustenance'),
('express', 'sociable', 'supporting'),
...
('special', 'societal', 'living'),
('special', 'societal', 'bread_and_butter'),
('special', 'societal', 'sustenance'),
('special', 'societal', 'supporting'),
('special', 'societal', 'accompaniment'),
('special', 'societal', 'musical_accompaniment'),
('special', 'societal', 'backup'),
('special', 'societal', 'financial_support'),
('special', 'societal', 'funding'),
('special', 'societal', 'backing')]
我有一系列我标记化的短语。然后,我找到了每个单词的同义词,并将它们保存在以单词(即令牌)为键、同义词列表为值的字典中。我的目标是通过用同义词替换每个单词并创建新短语来生成新短语。
例如,我们有一个包含 3 个标记的短语。第一个 token (limited) 有 18 个同义词,第二个 token (social) 有 4 个同义词,最后一个 token (support) 有 16 个同义词。所以理论上我们可以通过将所有 3 个列表组合在一起得到 18 * 4 * 16 = 1,152
个新短语。
phrases = ['limited', 'social', 'support']
dictionary = {
'limited': ['express', 'limited', 'restrict', 'restrain', 'trammel', 'limit', 'bound', 'confine', 'throttle', 'circumscribe', 'specify', 'set', 'determine', 'define', 'fix', 'circumscribed', 'modified', 'special'],
'social': ['sociable', 'social', 'mixer', 'societal'],
'support': ['support', 'reinforcement', 'reenforcement', 'documentation', 'keep', 'livelihood', 'living', 'bread_and_butter', 'sustenance', 'supporting', 'accompaniment', 'musical_accompaniment', 'backup', 'financial_support', 'funding', 'backing']
}
new_phrases = [['express', 'sociable', 'support'], ['express', 'social', 'support'], ['express', 'mixer', 'support'], ['express', 'societal', 'support'], ..., [...]]
我的尝试是遍历每个列表中的项目,但我很难概念化如何将这 3 个列表组合在一起以生成类似于 new_phrases
的内容,如上面的代码块所示。
for word in phrases:
print("\nthe word is:", word)
print("list of synonyms is:", dictionary[word])
print("the list has", len(dictionary[word]), "elements")
for syn in dictionary[word]:
print("a synonmy is:", syn)
import itertools
new_phrases = list(itertools.product(*dictionary.values()))
>>> new_phrases
[('express', 'sociable', 'support'),
('express', 'sociable', 'reinforcement'),
('express', 'sociable', 'reenforcement'),
('express', 'sociable', 'documentation'),
('express', 'sociable', 'keep'),
('express', 'sociable', 'livelihood'),
('express', 'sociable', 'living'),
('express', 'sociable', 'bread_and_butter'),
('express', 'sociable', 'sustenance'),
('express', 'sociable', 'supporting'),
...
('special', 'societal', 'living'),
('special', 'societal', 'bread_and_butter'),
('special', 'societal', 'sustenance'),
('special', 'societal', 'supporting'),
('special', 'societal', 'accompaniment'),
('special', 'societal', 'musical_accompaniment'),
('special', 'societal', 'backup'),
('special', 'societal', 'financial_support'),
('special', 'societal', 'funding'),
('special', 'societal', 'backing')]