如何合并两个字符串列表中的重复项?
问题描述:
我对python(2.7)有点新,而且我很难做到这一点。如何合并两个字符串列表中的重复项?
我有以下列表:
animal = ['cat', 'cat', 'dog', 'dog', 'dog', 'horse']
names = ['cat_01', 'cat_02', 'dog_01', 'dog_02', 'dog_03', 'horse_01']
我想有以下(元组的它可能是一个列表或者一个字典)
new = {"cat":('cat_01','cat_02'), "dog":('dog_01','dog_02', 'dog_03'), "horse":('horse_01')}
如何最好地做到这一点?
答
假设你的列表进行排序,因为它们是在例如:
代码:
my_dict = {}
for animal, name in zip(animals, names):
my_dict.setdefault(animal, []).append(name)
print(my_dict)
给出:
{'horse': ['horse_01'], 'dog': ['dog_01', 'dog_02', 'dog_03'], 'cat': ['cat_01', 'cat_02']}
如果你需要的元组没有列出:
my_dict = {k: tuple(v) for k, v in my_dict.items()}
答
简短的解决方案使用列表理解:
animal = ['cat', 'cat', 'dog', 'dog', 'dog', 'horse']
names = ['cat_01', 'cat_02', 'dog_01', 'dog_02', 'dog_03', 'horse_01']
result = {a:tuple([n for n in names if a in n]) for a in animal}
print result
输出:
{'cat': ('cat_01', 'cat_02'), 'horse': ('horse_01',), 'dog': ('dog_01', 'dog_02', 'dog_03')}
答
您还可以使用groupby
从itertools
from itertools import groupby
my_dict = {}
for key, groups in groupby(zip(animal, names), lambda x: x[0]):
my_dict[key] = tuple(g[1] for g in groups)
这可能会快一点当你的名单增长。
这会改变'in'运算符的'str.startwith'。因为我正在处理不以我需要的字符串开头的文件路径。不管怎样,谢谢你! –
如果是这样,我已经改变了这个片段。它会工作 – RomanPerekhrest