按键然后按值(列表或元组?)对字典进行排序
Sorting a Dictionary by Key and then Value (list or tuple?)
我有字典
dict = {key: [a, b, c, d]}
key是str,a是数字,b是数字,c是数字,d是数字。
如何打印我的字典,首先按 d 中的数字排序,然后 按键中字符串的长度排序 (最大的第一)?
我试过用这样的东西...
for key in sorted(dict.items(), key=lambda x: (-len(x[0]), x[1][3])):
但我不断收到属性错误。
字典是无序集合,除非您使用 Python 3.6+。因此,我假设您想根据您的 2 个标准构造一个 OrderedDict
。为此,您可以使用 sorted
和自定义键,然后输入 OrderedDict
:
from collections import OrderedDict
d = {'ads': [4, 6, 1, 4], 'bs': [1, 3, 1, 9], 'cfsdg': [6, 1, 5, 4], 'ddsf': [5, 1, 6, 4]}
res = OrderedDict(sorted(d.items(), key=lambda x: (x[1][-1], -len(x[0]))))
OrderedDict([('cfsdg', [6, 1, 5, 4]),
('ddsf', [5, 1, 6, 4]),
('ads', [4, 6, 1, 4]),
('bs', [1, 3, 1, 9])])
(无耻的盗用jpp的example dict)
你可以这样做:
di = {'ads': [4, 6, 1, 4], 'bs': [1, 3, 1, 9], 'cfsdg': [6, 1, 5, 4], 'ddsf': [5, 1, 6, 4]}
print('\n'.join(map(str, sorted(di.items(), key=lambda t: (t[1][-1], -len(t[0]))))))
打印:
('cfsdg', [6, 1, 5, 4])
('ddsf', [5, 1, 6, 4])
('ads', [4, 6, 1, 4])
('bs', [1, 3, 1, 9])
您可以按照以下方式做一些事情:
>>> dictionary = {'aaaa': [1,2,2,3], 'aa': [1,2,2,3], 'aaa': [2,2,2,2], 'a': [4,2,2,6]}
>>> arr = sorted(dictionary.items(), key=lambda x: (-x[1][3],-len(x[0])))
>>> for k in arr:
print(k)
('a', [4, 2, 2, 6])
('aaaa', [1, 2, 2, 3])
('aa', [1, 2, 2, 3])
('aaa', [2, 2, 2, 2])
注意:先按较大 d
排序,然后按较长 key
排序。
要反转任何优先级的顺序,只需在 lambda
函数的元组中的元素中尝试 removing/leaving -
。
您的代码有效,但可能不是您正在寻找的 "right" 行为,因为您的负号有误。你可能对你的 "key" 变量做错了什么,因为当你用 dict.items()
排序时,它 return 给你一个 (key, value)
的元组
这是一个不可能的事情,但我猜你正在尝试这样做?
for key in sorted(dict.items(), key=lambda x: (-len(x[0]), x[1][3])):
print(dict[key])
这是不正确的,因为 "key" return 是一个元组。
要以您更容易理解的方式执行此操作,将是以下内容。
for key, value in sorted(d.items(), key=lambda x: (x[1][-1], -len(x[0]))):
print(key, value)
为什么它 return 是一个元组列表?因为 dict.items()
是元组列表,而您正在对该元组列表进行排序,而不是字典对象。
您可以保持字典完整并对其进行排序,使用 dict()
构造函数
d = dict(sorted(d.items(), key=lambda x: (x[1][3], -len(x[0]))))
# {'stampede': [4, 2, 1, 1], 'the': [3, 3, 2, 1], 'vash': [1, 2, 3, 4]}
我有字典
dict = {key: [a, b, c, d]}
key是str,a是数字,b是数字,c是数字,d是数字。
如何打印我的字典,首先按 d 中的数字排序,然后 按键中字符串的长度排序 (最大的第一)?
我试过用这样的东西...
for key in sorted(dict.items(), key=lambda x: (-len(x[0]), x[1][3])):
但我不断收到属性错误。
字典是无序集合,除非您使用 Python 3.6+。因此,我假设您想根据您的 2 个标准构造一个 OrderedDict
。为此,您可以使用 sorted
和自定义键,然后输入 OrderedDict
:
from collections import OrderedDict
d = {'ads': [4, 6, 1, 4], 'bs': [1, 3, 1, 9], 'cfsdg': [6, 1, 5, 4], 'ddsf': [5, 1, 6, 4]}
res = OrderedDict(sorted(d.items(), key=lambda x: (x[1][-1], -len(x[0]))))
OrderedDict([('cfsdg', [6, 1, 5, 4]),
('ddsf', [5, 1, 6, 4]),
('ads', [4, 6, 1, 4]),
('bs', [1, 3, 1, 9])])
(无耻的盗用jpp的example dict)
你可以这样做:
di = {'ads': [4, 6, 1, 4], 'bs': [1, 3, 1, 9], 'cfsdg': [6, 1, 5, 4], 'ddsf': [5, 1, 6, 4]}
print('\n'.join(map(str, sorted(di.items(), key=lambda t: (t[1][-1], -len(t[0]))))))
打印:
('cfsdg', [6, 1, 5, 4])
('ddsf', [5, 1, 6, 4])
('ads', [4, 6, 1, 4])
('bs', [1, 3, 1, 9])
您可以按照以下方式做一些事情:
>>> dictionary = {'aaaa': [1,2,2,3], 'aa': [1,2,2,3], 'aaa': [2,2,2,2], 'a': [4,2,2,6]}
>>> arr = sorted(dictionary.items(), key=lambda x: (-x[1][3],-len(x[0])))
>>> for k in arr:
print(k)
('a', [4, 2, 2, 6])
('aaaa', [1, 2, 2, 3])
('aa', [1, 2, 2, 3])
('aaa', [2, 2, 2, 2])
注意:先按较大 d
排序,然后按较长 key
排序。
要反转任何优先级的顺序,只需在 lambda
函数的元组中的元素中尝试 removing/leaving -
。
您的代码有效,但可能不是您正在寻找的 "right" 行为,因为您的负号有误。你可能对你的 "key" 变量做错了什么,因为当你用 dict.items()
排序时,它 return 给你一个 (key, value)
这是一个不可能的事情,但我猜你正在尝试这样做?
for key in sorted(dict.items(), key=lambda x: (-len(x[0]), x[1][3])):
print(dict[key])
这是不正确的,因为 "key" return 是一个元组。
要以您更容易理解的方式执行此操作,将是以下内容。
for key, value in sorted(d.items(), key=lambda x: (x[1][-1], -len(x[0]))):
print(key, value)
为什么它 return 是一个元组列表?因为 dict.items()
是元组列表,而您正在对该元组列表进行排序,而不是字典对象。
您可以保持字典完整并对其进行排序,使用 dict()
构造函数
d = dict(sorted(d.items(), key=lambda x: (x[1][3], -len(x[0]))))
# {'stampede': [4, 2, 1, 1], 'the': [3, 3, 2, 1], 'vash': [1, 2, 3, 4]}