I have a dictionary
d = {(1,100) : 0.5 , (1,150): 0.7 ,(1,190) : 0.8, (2,100) : 0.5 , (2,120): 0.7 ,(2,150) : 0.8, (3,100) : 0.5 , (3,110): 0.7 ,(4,100) : 0.5 , (4,150): 0.7 ,(4,190) : 0.8,(5,100) : 0.5 , (5,150): 0.7}
list = [4,2,1,3,5]
for (k1,k2),k3 in d.items():
for k1 in list :
print(k1,k2 : ,k3)
I want get the value of dictionary sequential like my list for the key 1 and for key 2 I have diferrent score and count
(4,100) : 0.5 , (4,150): 0.7 ,(4,190) : 0.8,(2,100) : 0.5 , (2,120): 0.7 ,(2,150) : 0.8,(1,100) : 0.5 , (1,150): 0.7 ,(1,190) : 0.8,(3,100) : 0.5 , (3,110): 0.7 ,(5,100) : 0.5 , (5,150): 0.7}
CodePudding user response:
You can use sorted()
with the the values from the tuple as index in the list
d = dict(sorted(d.items(), key=lambda x: lst.index(x[0][0])))
print(d)
Output
{(4, 100): 0.5, (4, 150): 0.7, (4, 190): 0.8, (2, 100): 0.5, (2, 120): 0.7, (2, 150): 0.8, (1, 100): 0.5, (1, 150): 0.7, (1, 190): 0.8, (3, 100): 0.5, (3, 110): 0.7, (5, 100): 0.5, (5, 150): 0.7}
CodePudding user response:
Try the following:
d = {(1, 100): 0.5,
(1, 150): 0.5,
(1, 190): 0.8,
(2, 100): 0.5,
(2, 120): 0.7,
(2, 150): 0.8,
(3, 100): 0.5,
(3, 110): 0.7,
(4, 100): 0.5,
(4, 150): 0.7,
(4, 190): 0.8,
(5, 100): 0.5,
(5, 150): 0.7}
lst = [4, 2, 1, 3, 5]
for key, k3 in d.items():
print(f'({lst[key[0]-1]},{key[1]}) : ,{k3}')
Output:
(4,100) : ,0.5
(4,150) : ,0.5
(4,190) : ,0.8
(2,100) : ,0.5
(2,120) : ,0.7
(2,150) : ,0.8
(1,100) : ,0.5
(1,110) : ,0.7
(3,100) : ,0.5
(3,150) : ,0.7
(3,190) : ,0.8
(5,100) : ,0.5
(5,150) : ,0.7