Home > Blockchain >  How do I use python to turn two lists into a dictionary, and the value is the maximum value
How do I use python to turn two lists into a dictionary, and the value is the maximum value

Time:09-01

I have two lists here

L = ["a","b","b","c","c","c"]
L_02 = [1,3,2,4,6,""]

I want to turn two lists into a dictionary, and the value is the maximum value

dic = {"a":1,"b":3,"c":6}

how can I do this?

CodePudding user response:

We can first get the indices of each element in the list, get the corresponding values in the second list, and find the maximums and make a dictionary.

dic = {}
for element in set(L):
    indices = [i for i, x in enumerate(L) if x == element]
    corresponding = []
    for i in indices:
        if type(L_02[i]) == int:
            corresponding.append(L_02[i])
    value = max(corresponding)
    dic[element] = value
  • Related