2016-11-08 74 views
3

我导入CSV,在第二列有重复值,并在第1列最好的方法,比较了在清单列表,并与Python

64052,10.10.10.10,RED 
3802,192.168.10.10,BLUE 
488,10.10.10.10,RED 

添加相应的值添加不同的值我已经导入了CSV值如下列出的清单:

import csv 

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

print data 

[ '64052','10 .10.10.10' , '红'],[ '3802', '192.168.10.10' ,'BLUE'], ['488','10.10.10.10','RED']

什么是最好的方式来通过列表,如果“第二”[1]值匹配,添加值“第一”[0]?

这将是预期的输出,我试图完成:

[ '64540','10 .10.10.10' , '红'],[ '3802', '192.168.10.10', '蓝']

+0

声明'如果‘第二’[1]的值匹配,添加值‘第一’[0]'是没有意义的任何,并且没有可见的关系在输入和所需输出之间。 –

+0

有一个相关性 - 如果IP匹配,添加第一列 – matino

+0

是的,如果IP匹配,我想在第一列中添加值。 – rmat333

回答

1

你可以使用defaultdict跟踪记录:

from collections import defaultdict 

result = defaultdict(int) 
for row in data: 
    key = (row[1], row[2]) 
    result[key] += int(row[0]) 

然后你会看到如下这样:

{ 
    ('10.10.10.10', 'RED'): 64540, 
    ('192.168.10.10', 'BLUE'): 3802, 
} 
+0

这对我有效。我会发布我的最终结果。致力于将dict输出为我的原始格式的csv。 – rmat333

2

您可以轻松地做到这一点使用大熊猫

import pandas as 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']) 
# You can import the whole .csv file using the .read_csv() method 

df['Value'] = df['Value'].astype(int) # Cast to integers 
df.groupby(['IP', 'Color']).sum() 

结果

In[39]: df.groupby(['IP', 'Color']).sum() 
Out[37]: 
        Value 
IP   Color  
10.10.10.10 RED 64540 
192.168.10.10 BLUE 3802 

然后检索列表中的元组使用迭代器.itertuples()

0

你可以做这样的事情(假设数字在索引0是一个数字,而不是一个字符串,否则你就会有一个它转换成一个数字)

data = [ 
    (64052,'10.10.10.10','RED'), 
    (3802, '192.168.10.10','BLUE'), 
    (488, '10.10.10.10','RED'), 
] 

ip_to_datum_map = {} 

for datum in data: 
    number, ip_address, colour = datum 
    if ip_address in ip_to_datum_map: 
     existing_number = ip_to_datum_map[ip_address][0] 
     ip_to_datum_map[ip_address] = (existing_number + number, ip_address, colour) 

    else: 
     ip_to_datum_map[ip_address] = datum 

print(ip_to_datum_map.values()) 
0

你可以试试这个。它适用于: l = [[''500','192.168.10.100','RED'],['64052','10.10.10.10','RED'],['3802','192.168。 10.10','BLUE'],['488','10.10.10.10','RED'],['488','10.10.10.10','RED'],['500','192.168.10.10' ,'RED']]

输出=> [[65028,'10.10.10.10','RED'],[4302,'192.168.10.10','BLUE'],['500','192.168 .10.100' ,‘红’]

data = [['500', '192.168.10.100', 'RED'],['64052', '10.10.10.10', 'RED'], ['3802', '192.168.10.10', 'BLUE'], ['488', '10.10.10.10', 'RED'],['488', '10.10.10.10', 'RED'],['500', '192.168.10.10', 'RED']] 


key_l=[] 
final_list=[] 
l_index=[] 
my_dict={} 
for i in range(len(data)): 
    next_val = i+1 
    if next_val > len(data): 
     break 
    k=i+1 
    key_l=[] 
    for j in data[i+1:]: 
     if j[1] == data[i][1]: 
      if my_dict: 
       if j[1] in my_dict.keys(): 
        k=k+1 
        continue 
       else: 
        key_l.append(k) 
        k=k+1 
        continue 
      else: 
       key_l.append(k) 
       k=k+1 
     else: 
      k=k+1 
    if key_l: 
     key_l.append(data.index(data[i])) 
     #make a dictionary with ip as key and values = indexes from list containing this ip 
     my_dict[(data[i][1])] = key_l 

#iterate over each key and add the 0th value of each list element found at data[index]   
for each_key in my_dict.keys(): 
    new_val = 0 
    for value in my_dict[each_key]: 
     new_val += int(data[value][0])     

    data[value][0] = new_val 
    new_l = data[value] 
    final_list.append(new_l) 

for val in my_dict.values(): 
    l_index += val 

#add those list elements which are left 
for i in range(len(data)): 
    if i in l_index: 
     continue 
    else: 
     final_list.append(data[i]) 

print final_list