【问题标题】:Merging lists with similar value合并具有相似值的列表
【发布时间】:2018-12-25 19:34:40
【问题描述】:

我有一个包含汉密尔顿歌词的列表列表,每个列表中的第一个单词是演唱者的名字。我正在尝试在新列表中组合具有相同第一个单词(名称)的列表。该列表的名称称为 ham_sep_list,如下所示:

[['1', 'ACT', '1', '1', 'Alexander', 'Hamilton'], ['BURR', 'How', 'does', 'a', 'bastard', 'orphan', 'son', 'of', 'a', 'whore', 'and', 'a', 'Scotsman', 'dropped', 'in', 'the', 'middle', 'of', 'a', 'forgotten', 'spot', 'in', 'the', 'Caribbean', 'by', 'providence', 'impoverished', 'in', 'squalor', 'grow', 'up', 'to', 'be', 'a', 'hero', 'and', 'a', 'scholar'], ['LAURENS', 'The', 'ten', 'dollar', 'founding', 'father', 'without', 'a', 'father', 'got', 'a', 'lot', 'farther', 'by', 'working', 'a', 'lot', 'harder', 'by', 'being', 'a', 'lot', 'smarter', 'by', 'being', 'a', 'self', 'starter', 'by', 'fourteen', 'they', 'placed', 'him', 'in', 'charge', 'of', 'a', 'trading', 'charter'], ['JEFFERSON', 'And', 'every', 'day', 'while', 'slaves', 'were', 'being', 'slaughtered', 'and', 'carte', 'd', 'away', 'across', 'the', 'waves', 'he', 'struggled', 'and', 'kept', 'his', 'guard', 'up', 'Inside', 'he', 'was', 'longing', 'for', 'something', 'to', 'be', 'a', 'part', 'of', 'the', 'brother', 'was', 'ready', 'to', 'beg', 'steal', 'borrow', 'or', 'barter'], ['MADISON', 'Then', 'a', 'hurricane', 'came', 'and', 'devastation', 'reigned', 'our', 'man', 'saw', 'his', 'future', 'drip', 'dripping', 'down', 'the', 'drain', 'put', 'a', 'pencil', 'to', 'his', 'temple', 'connected', 'it', 'to', 'his', 'brain', 'and', 'he', 'wrote', 'his', 'first', 'refrain', 'a', 'testament', 'to', 'his', 'pain'], ['BURR', 'Well', 'the', 'word', 'got', 'around', 'they', 'said', 'fiThis', 'kid', 'is', 'insane', 'manfl', 'took', 'up', 'a', 'collection', 'just', 'to', 'send', 'him', 'to', 'the', 'mainland', 'fiGet', 'your', 'education', 'don™t', 'forget', 'from', 'whence', 'you', 'came', 'and', 'the', 'world', 'is', 'gonna', 'know', 'your', 'name', 'What™s', 'your', 'name', 'manfl'], ['HAMILTON', 'Alexander', 'Hamilton', 'My', 'name', 'is', 'Alexander', 'Hamilton', 'And', 'there™s', 'a', 'million', 'things', 'I', 'haven™t', 'done', 'but', 'just', 'you', 'wait', 'just', 'you', 'wait']]

ham_sep_list1 = collections.defaultdict(list)

for name, words in ham_sep_list:

    ham_sep_list[name].append(ham_sep_list)

print(ham_sep_list1)  

我想我正在寻找类似于上面显示的代码但可以接受超过 2 个值的解决方案...例如,“BURR”列表将包含“BURR”所说的所有单词。

谢谢!

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    只需对您的代码稍作修改,您就可以使用iterable unpacking 遍历各行以从每一行获取namewords。然后,您可以将words(这是一个列表)附加到您的defaultdict 以获取适当的name

    from collections import defaultdict
    
    lines = [['1', 'ACT', '1', '1', 'Alexander', 'Hamilton'], ['BURR', 'How', 'does', 'a', 'bastard', 'orphan', 'son', 'of', 'a', 'whore', 'and', 'a', 'Scotsman', 'dropped', 'in', 'the', 'middle', 'of', 'a', 'forgotten', 'spot', 'in', 'the', 'Caribbean', 'by', 'providence', 'impoverished', 'in', 'squalor', 'grow', 'up', 'to', 'be', 'a', 'hero', 'and', 'a', 'scholar'], ['LAURENS', 'The', 'ten', 'dollar', 'founding', 'father', 'without', 'a', 'father', 'got', 'a', 'lot', 'farther', 'by', 'working', 'a', 'lot', 'harder', 'by', 'being', 'a', 'lot', 'smarter', 'by', 'being', 'a', 'self', 'starter', 'by', 'fourteen', 'they', 'placed', 'him', 'in', 'charge', 'of', 'a', 'trading', 'charter'], ['JEFFERSON', 'And', 'every', 'day', 'while', 'slaves', 'were', 'being', 'slaughtered', 'and', 'carte', 'd', 'away', 'across', 'the', 'waves', 'he', 'struggled', 'and', 'kept', 'his', 'guard', 'up', 'Inside', 'he', 'was', 'longing', 'for', 'something', 'to', 'be', 'a', 'part', 'of', 'the', 'brother', 'was', 'ready', 'to', 'beg', 'steal', 'borrow', 'or', 'barter'], ['MADISON', 'Then', 'a', 'hurricane', 'came', 'and', 'devastation', 'reigned', 'our', 'man', 'saw', 'his', 'future', 'drip', 'dripping', 'down', 'the', 'drain', 'put', 'a', 'pencil', 'to', 'his', 'temple', 'connected', 'it', 'to', 'his', 'brain', 'and', 'he', 'wrote', 'his', 'first', 'refrain', 'a', 'testament', 'to', 'his', 'pain'], ['BURR', 'Well', 'the', 'word', 'got', 'around', 'they', 'said', 'fiThis', 'kid', 'is', 'insane', 'manfl', 'took', 'up', 'a', 'collection', 'just', 'to', 'send', 'him', 'to', 'the', 'mainland', 'fiGet', 'your', 'education', 'don™t', 'forget', 'from', 'whence', 'you', 'came', 'and', 'the', 'world', 'is', 'gonna', 'know', 'your', 'name', 'What™s', 'your', 'name', 'manfl'], ['HAMILTON', 'Alexander', 'Hamilton', 'My', 'name', 'is', 'Alexander', 'Hamilton', 'And', 'there™s', 'a', 'million', 'things', 'I', 'haven™t', 'done', 'but', 'just', 'you', 'wait', 'just', 'you', 'wait']]
    ham_sep_list = defaultdict(list)
    
    for name, *words in lines:
        ham_sep_list[name].append(words)
    
    print(ham_sep_list)
    

    输出

    {
      'LAURENS': [
        ['The', 'ten', 'dollar', 'founding', 'father', 'without', 'a', 'father', 'got', 'a', 'lot', 'farther', 'by', 'working', 'a', 'lot', 'harder', 'by', 'being', 'a', 'lot', 'smarter', 'by', 'being', 'a', 'self', 'starter', 'by', 'fourteen', 'they', 'placed', 'him', 'in', 'charge', 'of', 'a', 'trading', 'charter']
      ],
      'BURR': [
        ['How', 'does', 'a', 'bastard', 'orphan', 'son', 'of', 'a', 'whore', 'and', 'a', 'Scotsman', 'dropped', 'in', 'the', 'middle', 'of', 'a', 'forgotten', 'spot', 'in', 'the', 'Caribbean', 'by', 'providence', 'impoverished', 'in', 'squalor', 'grow', 'up', 'to', 'be', 'a', 'hero', 'and', 'a', 'scholar'],
        ['Well', 'the', 'word', 'got', 'around', 'they', 'said', 'fiThis', 'kid', 'is', 'insane', 'manfl', 'took', 'up', 'a', 'collection', 'just', 'to', 'send', 'him', 'to', 'the', 'mainland', 'fiGet', 'your', 'education', 'don™t', 'forget', 'from', 'whence', 'you', 'came', 'and', 'the', 'world', 'is', 'gonna', 'know', 'your', 'name', 'What™s', 'your', 'name', 'manfl']
      ],
      'HAMILTON': [
        ['Alexander', 'Hamilton', 'My', 'name', 'is', 'Alexander', 'Hamilton', 'And', 'there™s', 'a', 'million', 'things', 'I', 'haven™t', 'done', 'but', 'just', 'you', 'wait', 'just', 'you', 'wait']
      ],
      'MADISON': [
        ['Then', 'a', 'hurricane', 'came', 'and', 'devastation', 'reigned', 'our', 'man', 'saw', 'his', 'future', 'drip', 'dripping', 'down', 'the', 'drain', 'put', 'a', 'pencil', 'to', 'his', 'temple', 'connected', 'it', 'to', 'his', 'brain', 'and', 'he', 'wrote', 'his', 'first', 'refrain', 'a', 'testament', 'to', 'his', 'pain']
      ],
      'JEFFERSON': [
        ['And', 'every', 'day', 'while', 'slaves', 'were', 'being', 'slaughtered', 'and', 'carte', 'd', 'away', 'across', 'the', 'waves', 'he', 'struggled', 'and', 'kept', 'his', 'guard', 'up', 'Inside', 'he', 'was', 'longing', 'for', 'something', 'to', 'be', 'a', 'part', 'of', 'the', 'brother', 'was', 'ready', 'to', 'beg', 'steal', 'borrow', 'or', 'barter']
      ],
      '1': [
        ['ACT', '1', '1', 'Alexander', 'Hamilton']
      ]
    }
    

    【讨论】:

      【解决方案2】:

      如果您想在不使用 itertools 的情况下执行此操作,则可以选择

      d = dict()
      for i in your_list:
          d[i[0]] = set(d[i[0]]).union(set(i[1:]))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-25
        • 2021-10-29
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多