python add two list and createing a new list
Posted
by
Adam G.
on Stack Overflow
See other posts from Stack Overflow
or by Adam G.
Published on 2012-11-09T16:58:19Z
Indexed on
2012/11/09
16:59 UTC
Read the original article
Hit count: 272
python
|python-2.7
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 ']
output I need ==
['company1,AAA,2576.0','company1,ZZZ,-2576.0 ','company1,KKK,-400.0 ' etc etc]
I need to add it similar product number in each list and move it to a new list. I also need any symbol not being added together to be added to that new list. I am having problems with moving through each list.
This is what I have:
h = []
z = []
a = []
for g in hhl:
spl1 = g.split(",")
h.append(spl1[1])
for j in c_hhl:
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 hhl[:]:
spl4 = n.split(",")
if spl4[1] in a:
got = (spl4[0],spl4[1],spl4[2])
hhl.remove(n)
smash = hhl+z #for i in smash:
for i in smash:
print(i)
I am having problem iterating through the list to make sure I get all of the simliar product to a new list,(bold) and any product not in list 1 but in lst2 to the new list and vice versa. I am sure there is a much easier way.
© Stack Overflow or respective owner