我有3个列表,
a = ['Good', '24.97 %']
b = ['Bad', '75.03 %']
c = [('Amount', 0.20),
('Decision1', -0.149),
('Unit', 0.128),
('Decision2', -0.120),
('Decision3', 0.038),
('Matches', 0.016)]
好的对应于列表c中的负值,不好的对应于列表c中的值,所以我必须将其分离到一个新的列表中,例如
Good = ['Good', '24.97 %', ('Decision1', -0.149), ('Decision2', -0.120)]
Bad = ['Bad', '75.03 %', ('Amount', 0.20),('Unit', 0.128),('Decision3', 0.038),
('Matches', 0.016)]
答案 0 :(得分:5)
使用列表理解来获取底片和底片并将其附加到其他列表中:
good = a + [x for x in c if x[1] < 0]
bad = b + [x for x in c if x[1] > 0]
答案 1 :(得分:2)
使用以下代码获取结果:-
a = ['Good', '24.97 %']
b = ['Bad', '75.03 %']
c = [('Amount', 0.20),
('Decision1', -0.149),
('Unit', 0.128),
('Decision2', -0.120),
('Decision3', 0.038),
('Matches', 0.016)]
good = a.copy()
bad = b.copy()
for var in c:
if var[1] < 0:
good.append(var)
else:
bad.append(var)
print(good)
print(bad)
输出
['Good', '24.97 %', ('Decision1', -0.149), ('Decision2', -0.12)]
['Bad', '75.03 %', ('Amount', 0.2), ('Unit', 0.128), ('Decision3', 0.038), ('Matches', 0.016)]
希望对您有帮助。