Python比较元组列表和聚类单词列表

时间:2017-03-15 19:32:59

标签: python-2.7 list

我需要比较两种类型的列表。

带有频率的单词列表

list_1=[('psicomotricita',6), ('psicomotorio',5) , ('psicomotorie',6),('psicomotore', 7),
        ('bella',1), ('biella',7), ('bello',3),('zorro',4)]

列表列表,其中每个子列表都是由它们的相似性组成的词组。

list_2=[['psicomotricità', 'psicomotorio','psicomotorie','psicomotore']
        ['bella', 'biella', 'bello']
        ['zorro']]

因此,我需要循环list_2的每个子列表,以便在list_1中选择与最大频率进行比较的单词。

结果应为:

final_list['psicomotore','biella','zorro'] 

有没有人可以帮助我?谢谢!

1 个答案:

答案 0 :(得分:0)

经过长时间的努力(我是python中的新手)我解决了上面的问题。

所以,首先我将元组列表转换为字典:

d = {t[0]:int(t[1]) for t in list_1}

其次,我创建了以下功能:

def SortTuples(list, dict):
final_ls= []
ddd= []
for el in list_2:
    for key, value in d.iteritems():
        if key in el:
            ddd.append((key,value))
    z= (max(ddd,key=itemgetter(1))[0])
    final_ls.append(z)
    ddd= []
return final_ls

结果是一个列表,其中包含具有最大频率的单词:

Out: ['psicomotore', 'biella', 'zorro']