I'd like to process and print out the result value of the dictionary.

Asked 1 years ago, Updated 1 years ago, 99 views

** Character winning rate **
a : [4, 1, 0.8]
b : [6, 4, 0.6]
c : [0, 1, 0.0]
d : [5, 8, 0.38]

print("** character winning rate**")
for k,v in character_list3.items() :
    print(k,':',v)

I printed it out using the same code as above.
If the sum of values 1 and 2 among [value 1, value 2, value 3] in the list of value values does not exceed 3 I want to write a code that doesn't print out, and I want to list dictionary values in the order of size of value 3.

How should I approach it?

dictionary python

2022-09-22 13:12

1 Answers

dic = {
    'a': [4, 1, 0.8],
    'b': [6, 4, 0.6],
    'c': [0, 1, 0.0],
    'd': [5, 8, 0.38]
}

# Value1+value2 >=3 Filtering
filtered_dic = {k:v for k,v in dic.items() if v[0]+v[1] >= 3}

# Sort ascending by value 3
sorted_filtered_dic = sorted(filtered_dic.items(), key=lambda kv: kv[1][2])

# Output
print(sorted_filtered_dic)


2022-09-22 13:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.