Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

Appeding different list values to dictionary in python

I have three lists containing different pattern of values. This should append specific values only inside a single dictionary based on some if condition.I have tried the following way to do so but i got all the values from the list.

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe use 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)

Output:

Final_result {2: {'score': 0.98, 'box': [0.1, 0.2, 0.3, 0.4], 'class': 1}}

Edit:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...