0

希望你能用这个python函数帮助我:

def comparapal(lista):#lista is a list of lists where each list has 4 elements
  listaPalabras=[]
  for item in lista:
     if item[2] in eagles_dict.keys():# filter the list if the 3rd element corresponds to the key in the dictionary
        listaPalabras.append([item[1],item[2]]) #create a new list with elements 2 and 3

listaPalabras 结果:

[
   ['bien', 'NP00000'],
   ['gracia', 'NCFP000'],
   ['estar', 'VAIP1S0'],
   ['bien', 'RG'],
   ['huevo', 'NCMS000'],
   ['calcio', 'NCMS000'],
   ['leche', 'NCFS000'],
   ['proteina', 'NCFS000'],
   ['francisco', 'NP00000'],
   ['ya', 'RG'],
   ['ser', 'VSIS3S0'],
   ['cosa', 'NCFS000']
]

我的问题是:如何比较每个列表的第一个元素,以便如果单词相同,则比较它们的标签,即第二个元素。

抱歉含糊不清,该函数必须返回一个包含 3 个元素的列表列表:单词、标签和每个单词的出现次数。但是为了计算单词,我需要将单词与其他单词进行比较,如果存在 2 个或更多类似的单词,则比较标签以检查差异。如果标签不同,则分别计算单词。

结果 -> [['bien', 'NP00000',1],['bien', 'RG',1]] -> 两个相同的词,但通过标签的比较分别计算 提前致谢:

4

3 回答 3

2
import collections
inlist = [
   ['bien', 'NP00000'],
   ['gracia', 'NCFP000'],
   ['estar', 'VAIP1S0'],
   ['bien', 'RG'],
   ['huevo', 'NCMS000'],
   ['calcio', 'NCMS000'],
   ['leche', 'NCFS000'],
   ['proteina', 'NCFS000'],
   ['francisco', 'NP00000'],
   ['ya', 'RG'],
   ['ser', 'VSIS3S0'],
   ['cosa', 'NCFS000']
]
[(a,b,v) for (a,b),v in collections.Counter(map(tuple,inlist)).iteritems()]
#=>[('proteina', 'NCFS000', 1), ('francisco', 'NP00000', 1), ('ser', 'VSIS3S0', 1), ('bien', 'NP00000', 1), ('calcio', 'NCMS000', 1), ('estar', 'VAIP1S0', 1), ('huevo', 'NCMS000', 1), ('gracia', 'NCFP000', 1), ('bien', 'RG', 1), ('cosa', 'NCFS000', 1), ('ya', 'RG', 1), ('leche', 'NCFS000', 1)]

您想计算每对出现的次数。counter表达式就是这样做的。列表理解将其格式化为三元组。

于 2013-08-09T20:21:00.777 回答
1

你需要什么具体的输出?我不知道你到底需要做什么,但是如果你想对同一个词相关的项目进行分组,你可以把这个结构变成字典,以后再操作

>>> new = {}
>>> for i,j in a: # <-- a = listaPalabras 
        if new.get(i) == None:
                new[i] = [j]
        else:
                new[i].append(j)

这将给我们:

{'francisco': ['NP00000'], 'ser': ['VSIS3S0'], 'cosa': ['NCFS000'], 'ya': ['RG'], 'bien': ['NP00000', 'RG'], 'estar': ['VAIP1S0'], 'calcio': ['NCMS000'], 'leche': ['NCFS000'], 'huevo': ['NCMS000'], 'gracia': ['NCFP000'], 'proteina': ['NCFS000']}

然后你可以做:

>>> for i in new:
        if len(new[i]) > 1:
                print "compare {this} and {that}".format(this=new[i][0],that=new[i][1])

将打印:

compare NP00000 and RG #for key bien

编辑:在第一步中,您还可以使用 defaultdict,正如 Marcin 在评论中所建议的那样,这看起来像这样:

>>> d = defaultdict(list)
>>> for i,j in a:
        d.setdefault(i,[]).append(j)

EDIT2(回答OP的评论)

for i in d:
    item = []
    item.append(i)
    item.extend(d[i])
    item.append(len(d[i]))
    result.append(item)

这给了我们:

[['francisco', 'NP00000', 1], ['ser', 'VSIS3S0', 1], ['cosa', 'NCFS000', 1], ['ya', 'RG', 1], ['bien', 'NP00000', 'RG', 2], ['estar', 'VAIP1S0', 1], ['calcio', 'NCMS000', 1], ['leche', 'NCFS000', 1], ['huevo', 'NCMS000', 1], ['gracia', 'NCFP000', 1], ['proteina', 'NCFS000', 1]]
于 2013-08-09T19:43:19.867 回答
0

A purely list-based solution is possible of course, but requires additional looping. If efficiency is important, it might be better to replace listaPalabras with a dict.

def comparapal(lista):
  listaPalabras=[]
  for item in lista:
     if item[2] in eagles_dict.keys():
        listaPalabras.append([item[1],item[2]])

  last_tt = [None, None]
  for tt in sorted(listaPalabras):
    if tt == last_tt:
      print "Observed %s twice" % tt
    elif tt[0] == last_tt[0]:
      print "Observed %s and %s" % (tt, last_tt)
    last_tt = tt

This gives you: Observed ['bien', 'RG'] and ['bien', 'NP00000']

If this does not suit your purposes, please specify your question.

于 2013-08-09T19:54:25.913 回答