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 ']
我需要的输出==
['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)
我在遍历列表时遇到问题,以确保我将所有类似产品添加到新列表(粗体)以及不在列表 1 中但在 lst2 中的任何产品到新列表,反之亦然。我相信有一个更简单的方法。