两个list转换成dict
list_num = [1,2,3]
list_str = ['a','b','c']
print(dict(zip(list_num, list_str)))
两个字典合并:
dict1 = {'a':1, 'b':2, 'c':3}
dict2 = {'q':1, 'w':2, 'e':3}
print(dict(dict1, **dict2))
{'a': 1, 'b': 2, 'c': 3, 'q': 1, 'w': 2, 'e': 3}
字符串转换成dict
str ="{'a': 1, 'b': 2, 'c': 3}"
print(eval(str))
print(type(eval(str)))
<class 'dict'>
{'a': 1, 'b': 2, 'c': 3}
嵌套排序
以第二个索引元素排序
list_sort1 = [(1, 2), (2, 3), (3, 4)]
print(sorted(list_sort1, key=lambda x: x[1], reverse=True))
打印:[(3, 4), (2, 3), (1, 2)]
dict1 = {'d': 4, 'a': 1, 'b': 2, 'c': 3}
print(sorted(dict1.items(), key=lambda x: x[1], reverse=False))
打印:[('a', 1), ('b', 2), ('c', 3), ('d', 4)]