【问题标题】:Generating new values by combining two lists in Python通过在 Python 中组合两个列表来生成新值
【发布时间】:2021-05-06 21:12:25
【问题描述】:

我有一系列标记化的短语。然后我找到了每个单词的同义词,并将它们保存在字典中,单词(即令牌)作为键,同义词列表作为值。我的目标是通过用同义词替换每个单词来生成新短语并创建新短语。

例如,我们有一个包含 3 个标记的短语。第一个标记(有限)有 18 个同义词,第二个标记(社交)有 4 个同义词,最后一个标记(支持)有 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)

【问题讨论】:

    标签: python for-loop nlp data-augmentation


    【解决方案1】:
    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')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2021-02-09
      • 2022-06-17
      相关资源
      最近更新 更多