2012-11-09 123 views
1
lst1 = ['company1,AAA,7381.0 ', 'company1,BBB,-8333.0 ', 'company1,CCC, 
3079.999 ', 'company1,DDD,5699.0 ', 'company1,EEE,1640.0 ', 
     'company1,FFF,-600.0 ', 'company1,GGG,3822.0 ', 'company1,HHH,-600.0 ', 
     'company1,JJJ,-4631.0 ', 'company1,KKK,-400.0 '] 

lst2 =['company1,AAA,-4805.0 ', 'company1,ZZZ,-2576.0 ', 'company1,BBB,1674.0 ', 'company1,CCC,3600.0 ', 'company1,DDD,1743.998 '] 

輸出我需要==蟒蛇添加兩個列表和createing一個新的列表

['company1,AAA,2576.0','company1,ZZZ,-2576.0 ','company1,KKK,-400.0 ' etc etc] 

我需要添加它類似的產品數量在每個列表並將其移動到一個新的列表。我還需要將任何符號添加到新列表中。我在瀏覽每個列表時遇到問題。

這是我有:

h = [] 

z = []   

a = []   

for g in lst1: 
    spl1 = g.split(",") 
    h.append(spl1[1]) 
for j in lst2: 
    spl2 = j.split(",") 
    **if spl2[1] in h: 
     converted_num =(float(spl2[2]) +float(spl1[2])) 
     pos=('{0},{1},{2}'.format(spl2[0],spl2[1],converted_num)) 
     z.append(pos)** 
    else: 
     pos=('{0},{1},{2}'.format(spl2[0],spl2[1],spl2[2])) 
     z.append(pos) 

for f in z: 
    spl3 = f.split(",") 
    a.append(spl3[1]) 

for n in lst1[:]: 
    spl4 = n.split(",") 
    if spl4[1] in a: 
     got = (spl4[0],spl4[1],spl4[2]) 
     lst1.remove(n) 
smash = lst1+z #for i in smash: 
for i in smash: 
    print(i) 

我有問題通過列表迭代,以確保我得到的所有simliar產品的一個新的列表,(粗體)和任何產品不在列表中的1但是在第一個到新的列表中,反之亦然。我相信有一個更簡單的方法。

+3

燦」你在這裏用字典嗎? –

+0

而不是'spl2 = j.split(「,」)'你可能想要做'公司名稱,字母,值= j.split(「,」)' –

+0

我可以,但我需要最終的輸出在列表中與所有的產品和金額,如lst1和lst2以上 –

回答

2

我第二次使用字典的建議。因爲我很懶,所以我更喜歡使用defaultdict,因爲那樣我就不用擔心檢查密鑰是否存在。 (你也可以在這裏使用一個Counter了。)特別是,假設你要開始與你的清單結尾:

from collections import defaultdict 

data = defaultdict(float) 

for line in lst1+lst2: 
    name, code, value = line.split(",") 
    data[name, code] += float(value) 

newlist = ['{},{},{}'.format(key[0], key[1], val) for key, val in sorted(data.items())] 

>>> data 
defaultdict(<type 'float'>, {('company1', 'HHH'): -600.0, ('company1', 'JJJ'): -4631.0, 
('company1', 'KKK'): -400.0, ('company1', 'DDD'): 7442.998, ('company1', 'ZZZ'): -2576.0, 
('company1', 'CCC'): 6679.999, ('company1', 'AAA'): 2576.0, ('company1', 'FFF'): -600.0, 
('company1', 'GGG'): 3822.0, ('company1', 'EEE'): 1640.0, ('company1', 'BBB'): -6659.0}) 

>>> newlist 
['company1,AAA,2576.0', 'company1,BBB,-6659.0', 'company1,CCC,6679.999', 
'company1,DDD,7442.998', 'company1,EEE,1640.0', 'company1,FFF,-600.0', 
'company1,GGG,3822.0', 'company1,HHH,-600.0', 'company1,JJJ,-4631.0', 
'company1,KKK,-400.0', 'company1,ZZZ,-2576.0'] 
+0

謝謝!!!!!!!! –

1

您應該使用一個字典映射串花車,像這樣:

dict1 = {'company1,AAA':7381.0, 'company1,BBB':-8333.0, 'company1,CCC':3079.999} 

那麼當你得到一個新的字典:

dict2 = {'company1,AAA':-4805.0} 

你可以說:

for key in dict2.keys(): 
    dict1[key] = dict1[key] + dict2[key] 

或沿着這些線(也請參閱function .update())

編輯:

的.update()函數也將讓你更新與dict2新項目的字典,只用簡單的if語句檢查,如果從dict2關鍵是不是已經在dict1

0

一個完整的解決方案你的問題

from collections import OrderedDict 
lst1 = ['company1,AAA,7381.0 ', 'company1,BBB,-8333.0 ', 'company1,CCC, 3079.999 ', 
     'company1,DDD,5699.0 ', 'company1,EEE,1640.0 ', 'company1,FFF,-600.0 ', 
     'company1,GGG,3822.0 ', 'company1,HHH,-600.0 ', 'company1,JJJ,-4631.0 ', 
     'company1,KKK,-400.0 '] 
lst2 =['company1,AAA,-4805.0 ', 'company1,ZZZ,-2576.0 ', 'company1,BBB,1674.0 ', 
     'company1,CCC,3600.0 ', 'company1,DDD,1743.998 '] 
#Create an OrderedDict, this will ensure that the Order in the original 
# List is maintained 
out_dict = OrderedDict() 
#Create a Dictionary out of the first List, Note the key is 
#the company and product ID 
for e in lst1: 
    key, _, value = e.strip().rpartition(",") 
    out_dict[key] = float(value) 
#Now iterate through the next list 
for e in lst2: 
# Partition on company, productID and Value 
    key, _, value = e.strip().rpartition(",") 
#Retrieve the value with the key with default '0' 
#and add value from the new list 
    out_dict[key] = out_dict.get(key, 0) + float(value) 
#Finally recreate the list from the dictionary 
lst3 = [','.join(map(str, e)) for e in out_dict.iteritems()] 
print lst3