sorted
列表排序有两种方法实现,一种是列表自带的sort()方法,即list.sort(),另一种则是通过sorted()进行排序
list.sort(key=None, reverse=False)
- key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
- reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认
该方法没有返回值,会改变原始列表的顺序。
sorted(iterable, key=None, reverse=False)
iterable -- 可迭代对象。
key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
返回一个新的列表。
元素是元组或列表
data = [(1, 2), (4, 1), (9, 8), (3, 4)] # 对元组的第2个元素进行排序 data.sort(key=lambda k: k[1]) print('data:', data) # 执行结果:data: [(4, 1), (1, 2), (3, 4), (9, 8)] # 对元组的第1个元素进行排序 print('data:', sorted(data)) # 执行结果:data: [(1, 2), (3, 4), (4, 1), (9, 8)]
元素是字典
data = [{'a': 2}, {'b': 1}, {'c': 9}, {'d': 3}]
# 对字典的值进行排序
data.sort(key=lambda k: list(k.values()))
print('data:', data)
# 执行结果:data: [{'b': 1}, {'a': 2}, {'d': 3}, {'c': 9}]
# 对字典的键进行排序
print('data:', sorted(data, key=lambda k: list(k.keys())))
# 执行结果:data: [{'a': 2}, {'b': 1}, {'c': 9}, {'d': 3}]
元素是嵌套字典
data = [{'a': {'num': 2, 'text': 'a'}}, {'b': {'num': 1, 'text': 'b'}}, {'c': {'num': 9, 'text': 'c'}},
{'d': {'num': 3, 'text': 'd'}}]
# 对嵌套字典中的值进行排序
data.sort(key=lambda k: [i['num'] for i in list(k.values())])
print('data:', data)
# 执行结果:data: [{'b': {'num': 1, 'text': 'b'}}, {'a': {'num': 2, 'text': 'a'}}, {'d': {'num': 3, 'text': 'd'}},
# {'c': {'num': 9, 'text': 'c'}}]
# 对嵌套字典中的值进行排序
print('data:', sorted(data, key=lambda k: [i['text'] for i in list(k.values())]))
# 执行结果:data: [{'a': {'num': 2, 'text': 'a'}}, {'b': {'num': 1, 'text': 'b'}}, {'c': {'num': 9, 'text': 'c'}},
# {'d': {'num': 3, 'text': 'd'}}]
自定义函数指定列表中的元素进行排序
data = [{'a': {'num': 2, 'text': 'a'}}, {'b': {'num': 1, 'text': 'b'}}, {'c': {'num': 9, 'text': 'c'}},
{'d': {'num': 3, 'text': 'd'}}]
def key(data):
key_list = []
for i in list(data.values()):
key_list.append(i['num'])
return key_list
# 对嵌套字典中的值进行排序
data.sort(key=key)
print('data:', data)
# 执行结果:data: [{'b': {'num': 1, 'text': 'b'}}, {'a': {'num': 2, 'text': 'a'}}, {'d': {'num': 3, 'text': 'd'}},
# {'c': {'num': 9, 'text': 'c'}}]
# 对嵌套字典中的值进行排序
print('data:', sorted(data, key=key))
# 执行结果:data: [{'b': {'num': 1, 'text': 'b'}}, {'a': {'num': 2, 'text': 'a'}}, {'d': {'num': 3, 'text': 'd'}},
# {'c': {'num': 9, 'text': 'c'}}]
字典排序
因为sorted(data.items())得到的是包含键、值的元组列表,即[('a', 2), ('b', 1), ('c', 9), ('d', 3)],所以可以通过sorted()函数进行排序得到新的列表,再通过字典解析得到字典。
元素是键值对
data = {'a': 2, 'b': 1, 'c': 9, 'd': 3}
# 对值进行排序
data_sorted = {i[0]: i[1] for i in sorted(data.items(), key=lambda k: k[1], reverse=True)}
print('data:', data_sorted)
# 执行结果:data: {'c': 9, 'd': 3, 'a': 2, 'b': 1}
元素是嵌套字典
data = {'a': {'num': 2, 'text': 'a'}, 'b': {'num': 1, 'text': 'b'}, 'c': {'num': 9, 'text': 'c'},
'd': {'num': 3, 'text': 'd'}}
# 对嵌套字典中的值进行排序
data_sorted = {i[0]: i[1] for i in sorted(data.items(), key=lambda k: k[1]['num'])}
print('data:', data_sorted)
# 执行结果:data: {'b': {'num': 1, 'text': 'b'}, 'a': {'num': 2, 'text': 'a'}, 'd': {'num': 3, 'text': 'd'},
# 'c': {'num': 9, 'text': 'c'}}
元素是列表嵌套字典
data = {'a': [{'num1': 2, 'text': 'a'}, {'num2': 3, 'text': 'd'}],
'b': [{'num1': 1, 'text': 'b'}, {'num2': 2, 'text': 'c'}],
'c': [{'num1': 9, 'text': 'c'}, {'num2': 1, 'text': 'b'}],
'd': [{'num1': 3, 'text': 'd'}, {'num2': 9, 'text': 'a'}]}
# 对嵌套字典中的第一个元素进行排序
data_sorted = {i[0]: i[1] for i in sorted(data.items(), key=lambda k: k[1][0]['num1'])}
print('data:', data_sorted)
# 执行结果:data: {'b': [{'num1': 1, 'text': 'b'}, {'num2': 2, 'text': 'c'}],
# 'a': [{'num1': 2, 'text': 'a'}, {'num2': 3, 'text': 'd'}],
# 'd': [{'num1': 3, 'text': 'd'}, {'num2': 9, 'text': 'a'}],
# 'c': [{'num1': 9, 'text': 'c'}, {'num2': 1, 'text': 'b'}]}
字典公共键
s1 = {x: randint(1, 4) for x in sample('abcdefg', randint(3, 6))}
s2 = {x: randint(1, 4) for x in sample('abcdefg', randint(3, 6))}
s3 = {x: randint(1, 4) for x in sample('abcdefg', randint(3, 6))}
print(list(map(dict.keys, [s1, s2, s3])))
print(reduce(lambda a, b: a & b, map(dict.keys, [s1, s2, s3])))
立志如山 静心求实
浙公网安备 33010602011771号