我有三个包含不同值模式的列表。这应该仅根据某些if条件在单个字典内附加特定值。我尝试了以下方法,但我从列表中得到了所有值。
class_list = [1,2,3,4,5,6]
boxes = [[0.1,0.2,0.3,0.4],[0.5,0.7,0.8,0.9],[0.7,0.9,0.4,0.2],[0.9,0.7,0.6,0.3],[0.9,0.14,0.6,0.3],[0.9,0.7,0.6,0.13]]
scores = [0.98,0.87,0.97,0.96,0.94,0.92]
k=1;
data = {}
for a in scores:
for b in boxes:
for c in list:
if a >= 0.98:
data[k+1] = {"score":a,"box":b, "class": c } ;
k=k+1;
print("Final_result",data)
答案 0 :(得分:4)
也许使用zip
:
for a,b,c in zip(scores,boxes,class_list):
if a >= 0.98:
data[k+1] = {"score":a,"box":b, "class": c } ;
k=k+1;
print("Final_result",data)
输出:
Final_result {2: {'score': 0.98, 'box': [0.1, 0.2, 0.3, 0.4], 'class': 1}}
编辑:
for a,b,c in zip(scores,boxes,class_list):
if a >= 0.98:
data[k+1] = {"score":a,"box":b, "class": int(c) } ;
k=k+1;
print("Final_result",data)
答案 1 :(得分:1)
获得所有值是因为您将所有其他值(即6x6x6次)遍历所有值。 我了解您的问题,您需要使用的是Zip功能。
llist = [1,2,3,4,5,6]
boxes = [[0.1,0.2,0.3,0.4],[0.5,0.7,0.8,0.9],[0.7,0.9,0.4,0.2],[0.9,0.7,0.6,0.3],[0.9,0.14,0.6,0.3],[0.9,0.7,0.6,0.13]]
scores = [0.98,0.87,0.97,0.96,0.94,0.92]
k=1;
data = {}
for a,b,c in zip(scores, boxes, llist):
if a >= 0.98:
data[k] = {"score":a,"box":b, "class": c } ;
k = k + 1
for k in data.keys():
print(k , data[k])
ps:避免使用关键字命名变量,列表是关键字。可能会遇到问题,甚至可以找到原因。