Useful snippets about Python

1. Inversely convert the key and value of the dictionary to the position

The value value can take any data type, but the key key must be immutable, such as a string, number or tuple.

dict1={'Lisa':1,'Bob':2,'Mick':3}

 

dict2=dict([(value,key) for (key,value) in dict1.items()])

 

dict2

{1: 'Lisa', 2: 'Bob', 3: 'Mick'}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324747236&siteId=291194637