Best way to compare a in a list of list and add a different value with python -


i'm importing csv has duplicate values in second column , add corresponding values in column 1.

64052,10.10.10.10,red 3802,192.168.10.10,blue 488,10.10.10.10,red 

i've imported csv values list of lists below:

import csv  out = open('example1.csv','rb') data = csv.reader(out) data = [[row[0],row[1],row[2]] row in data] out.close  print data 

['64052', '10.10.10.10', 'red'], ['3802', '192.168.10.10', 'blue'], ['488', '10.10.10.10', 'red']

what's best way go through lists , if "second" [1] value matches, add values "first" [0]?

this expected output i'm trying accomplish:

['64540', '10.10.10.10', 'red'], ['3802', '192.168.10.10', 'blue']

you can using pandas :

import pandas pd df = pd.dataframe([('64052', '10.10.10.10', 'red'), ('3802', '192.168.10.10', 'blue'), ('488', '10.10.10.10', 'red')], columns = ['value', 'ip', 'color']) # can import whole .csv file using .read_csv() method  df['value'] = df['value'].astype(int) # cast integers df.groupby(['ip', 'color']).sum() 

result:

in[39]: df.groupby(['ip', 'color']).sum() out[37]:                       value ip            color        10.10.10.10   red    64540 192.168.10.10 blue    3802 

then retrieve tuples in list use iterator .itertuples()


Comments

Popular posts from this blog

How to get the arclen between two curve points in Maya? -

C++ Linked List -

git svn - Unignore directories in git-svn (adding directories to a sparse checkout) -