合并具有相似值的列表

时间:2018-12-25 19:34:41

标签: python python-3.x

我有一个列表列表,其中包含汉密尔顿的歌词,每个列表中的第一个单词是演唱者的名字。我正在尝试在新列表中合并具有相同第一个单词(名称)的列表。列表的名称称为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)  

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

谢谢!

2 个答案:

答案 0 :(得分:2)

只需对代码稍作修改,就可以在遍历各行时使用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']
  ]
}

答案 1 :(得分:0)

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

def thirdpoint(a, b, c):
    result = []
    y=((a**2)+(b**2)-(c**2))/(a*2)
    x = math.sqrt((b**2)-(y**2))
    result.append(x)
    result.append(y)
    return result
相关问题