我想删除第1列中的重复项,并在第2列中使用python返回与每个惟一项相关联的值的相关列表。你知道吗
输入为
1 2
Jack London 'Son of the Wolf'
Jack London 'Chris Farrington'
Jack London 'The God of His Fathers'
Jack London 'Children of the Frost'
William Shakespeare 'Venus and Adonis'
William Shakespeare 'The Rape of Lucrece'
Oscar Wilde 'Ravenna'
Oscar Wilde 'Poems'
而输出应该是
1 2
Jack London 'Son of the Wolf, Chris Farrington, Able Seaman, The God of His Fathers,Children of the Frost'
William Shakespeare 'The Rape of Lucrece,Venus and Adonis'
Oscar Wilde 'Ravenna,Poems'
其中第二列包含与每个项相关联的值的总和。 我在dictionary上尝试了set()函数
dic={'Jack London': 'Son of the Wolf', 'Jack London': 'Chris Farrington', 'Jack London': 'The God of His Fathers'}
set(dic)
但它只返回字典的第一个键
set(['Jack London'])
在Python中,字典每个键只能包含一个值。但该值可以是项目的集合:
要从键值对序列构造这样的字典,可以执行以下操作:
循环体可以变得更简洁,如下所示:
您应该使用
itertools.groupby
,因为您的列表已排序。你知道吗这将为您提供以下结果:
相关问题 更多 >
编程相关推荐