Counting elements in a list of lists in python -


hope u can me w/ python function:

def comparapal(lista):#lista list of lists each list has 4 elements   listapalabras=[]   item in lista:      if item[2] in eagles_dict.keys():# filter list if 3rd element corresponds key in dictionary         listapalabras.append([item[1],item[2]]) #create new list elements 2 , 3 

the listapalabras result:

[    ['bien', 'np00000'],    ['gracia', 'ncfp000'],    ['estar', 'vaip1s0'],    ['bien', 'rg'],    ['huevo', 'ncms000'],    ['calcio', 'ncms000'],    ['leche', 'ncfs000'],    ['proteina', 'ncfs000'],    ['francisco', 'np00000'],    ['ya', 'rg'],    ['ser', 'vsis3s0'],    ['cosa', 'ncfs000'] ] 

my question is: how can compare 1st element of each list if word same, compare tags 2nd element.

sorry being ambiguous, fuunction has return list of lists w/ 3 elements: word, tag , number of occurrences of each word. in order count words need compare word w/ others , if there exists 2 or more words alike, compare tags chk difference. if tags different count words separately.

result -> [['bien', 'np00000',1],['bien', 'rg',1]] -> 2 same words counted separately comparison of tags in advance:

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) (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)] 

you want count number of occurrences of each pair. counter expression that. list comprehension formats triples.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -