如何首先按字母顺序对字典进行排序,然后按值升序排列?

时间:2019-09-17 13:35:27

标签: python sorting dictionary

我有一个字典mydict,具有以下形状:

{'qui': [(4, 1), (3, 2), (10, 1)], 'voluptate': [(3, 1)], 'est': [(4, 1)], 'reprehenderit': [(3, 1)], 'cillum': [(3, 1), (9, 1)], 'irure': [(1, 1), (3, 1)], 'laboris': [(2, 1)], 'sit': [(1, 1)], 'sunt': [(4, 1)], 'eu': [(3, 1)], 'nisi': [(5, 1), (2, 1)]}

我需要先按字母顺序对键进行排序,然后完成操作,然后按元组中的第一个数字对元组进行排序。

我已经看到了对first by key then valuesorting alphabeticallysorting by value then key进行排序的资源,但是到目前为止,没有问题。

我尝试使用sorted_dict = sorted(d.keys()),但这只是按字母顺序而不是它们的值返回键的列表

如何实现如下目标结果?

{'cillum': [(3, 1), (9, 1)], 'est': [(4, 1)], 'eu': [(3, 1)], 'irure': [(1, 1), (3, 1)], 'laboris': [(2, 1)], 'nisi': [(2, 1), (5, 1)], 'qui': [(3, 2), (4, 1), (10, 1)], 'reprehenderit': [(3, 1)], 'sit': [(1, 1)], 'sunt': [(4, 1)], 'voluptate': [(3, 1)],}

python --version yields Python 3.6.7

4 个答案:

答案 0 :(得分:2)

@DeepSpace OP指出,OP使用Python 3.6。

因此,命令dict是实现的详细信息,我们必须使用OrderedDict。

这是我的版本:

from collections import OrderedDict

d = {'qui': [(4, 1), (3, 2), (10, 1)], 'voluptate': [(3, 1)], 'est': [(4, 1)], 'reprehenderit': [(3, 1)], 'cillum': [(3, 1), (9, 1)], 'irure': [(1, 1), (3, 1)], 'laboris': [(2, 1)], 'sit': [(1, 1)], 'sunt': [(4, 1)], 'eu': [(3, 1)], 'nisi': [(5, 1), (2, 1)]}

ordered_dict = OrderedDict()
sorted_keys = sorted(d.keys())

for key in sorted_keys:
    sorted_values = sorted(d[key])
    ordered_dict[key] = sorted_values

print(ordered_dict)

输出:

OrderedDict([('cillum', [(3, 1), (9, 1)]), ('est', [(4, 1)]), ('eu', [(3, 1)]), ('irure', [(1, 1), (3, 1)]), ('laboris', [(2, 1)]), ('nisi', [(2, 1), (5, 1)]), ('qui', [(3, 2), (4, 1), (10, 1)]), ('reprehenderit', [(3, 1)]), ('sit', [(1, 1)]), ('sunt', [(4, 1)]), ('voluptate', [(3, 1)])])

答案 1 :(得分:1)

为此,您首先需要对内部列表进行排序(直接在列表上使用sorted将根据其各自的元素对元组进行排序),然后对字典进行排序(此处为d)根据键并构建一个OrderedDict,以便确保最终字典中的顺序(字典中的顺序仅从Python 3.7> =得到保证,如@deepspace所述):

from collections import OrderedDict

out = {k:sorted(v) for k,v in d.items()}
OrderedDict(sorted(out.items(), key=itemgetter(0)))

{'cillum': [(3, 1), (9, 1)],
 'est': [(4, 1)],
 'eu': [(3, 1)],
 'irure': [(1, 1), (3, 1)],
 'laboris': [(2, 1)],
 'nisi': [(2, 1), (5, 1)],
 'qui': [(3, 2), (4, 1), (10, 1)],
 'reprehenderit': [(3, 1)],
 'sit': [(1, 1)],
 'sunt': [(4, 1)],
 'voluptate': [(3, 1)]}

答案 2 :(得分:1)

这可行,可能会更快一些:

import Collections  # For Python < 3.7

dict_unsorted = {'qui': [(4, 1), (3, 2), (10, 1)], 'voluptate': [(3, 1)], 'est': [(4, 1)], 'reprehenderit': [(3, 1)], 'cillum': [(3, 1), (9, 1)], 'irure': [(1, 1), (3, 1)], 'laboris': [(2, 1)], 'sit': [(1, 1)], 'sunt': [(4, 1)], 'eu': [(3, 1)], 'nisi': [(5, 1), (2, 1)]}

# dict_sorted = {}  # For Python 3.7+
dict_sorted = Collections.OrderedDict()  # For Python < 3.7

for element in sorted(dict_unsorted.keys()):
  sortedVal = sorted(dict_unsorted[element])
  dict_sorted[element] = sortedVal

print(dict_sorted)

答案 3 :(得分:0)

单线略短:

d = {'qui': [(4, 1), (3, 2), (10, 1)], 'voluptate': [(3, 1)], 'est': [(4, 1)], 'reprehenderit': [(3, 1)], 'cillum': [(3, 1), (9, 1)], 'irure': [(1, 1), (3, 1)], 'laboris': [(2, 1)], 'sit': [(1, 1)], 'sunt': [(4, 1)], 'eu': [(3, 1)], 'nisi': [(5, 1), (2, 1)]}
new_d = dict(sorted([(a, sorted(b)) for a, b in d.items()]))

输出:

{'cillum': [(3, 1), (9, 1)], 'est': [(4, 1)], 'eu': [(3, 1)], 'irure': [(1, 1), (3, 1)], 'laboris': [(2, 1)], 'nisi': [(2, 1), (5, 1)], 'qui': [(3, 2), (4, 1), (10, 1)], 'reprehenderit': [(3, 1)], 'sit': [(1, 1)], 'sunt': [(4, 1)], 'voluptate': [(3, 1)]}