我正在尝试对基于类似子字符串的值列表进行排序。我想将其分组到列表的字典中,其中键是相似的子字符串,值是这些分组值的列表。
例如(实际列表有 24k 个条目):
test_list = [ 'Doghouse Amsterdam', 'Doghouse Antwerp', 'Doghouse Vienna',
'House by KatSkill', 'Garden by KatSkill', 'Meadow by KatSkill']
至:
resultdict = {
'Doghouse' : ['Doghouse Amsterdam', 'Doghouse Antwerp', 'Doghouse Vienna'],
'by KatSkill' : [ 'House by KatSkill', 'Garden by KatSkill', 'Meadow by KatSkill' ]
}
我尝试了以下方法,但这根本不起作用。
from itertools import groupby
test_list = [ 'Doghouse Amsterdam', 'Doghouse Antwerp', 'Doghouse Vienna',
'House by KatSkill', 'Garden by KatSkill', 'Meadow by KatSkill']
res = [list(i) for j, i in groupby(test_list,
lambda a: a.partition('_')[0])]
汪汪一只猫
Qyouu
一只斗牛犬
相关分类