Python 如何先按字母顺序按键排序,然后按升序排序?
我有一本字典,Python 如何先按字母顺序按键排序,然后按升序排序?,python,sorting,dictionary,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)], '
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)]}
我需要首先按字母顺序对键进行排序,然后完成后,按元组中的第一个数字对元组进行排序
我已经看到了用于排序的资源,但到目前为止,没有任何问题存在这个问题
我尝试过使用sorted\u 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
为此,您首先需要对内部列表进行排序(直接使用列表上的
sorted
将根据元组各自的元素对元组进行排序),然后根据键对字典进行排序(d
),并构建一个orderedict
,以保证最终字典中的顺序(字典中的顺序仅由Python 3.7>=作为@deepspace提到的保证):
略短的一个衬里:
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)]}
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)])])
这是可行的,可能有更快的东西:
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)
import Collections#用于Python<3.7
dict_unsorted={'qui':[(4,1)、(3,2)、(10,1)],'voluptate':[(3,1)],'est':[(4,1)],'reprehenderit':[(3,1)],'cilum':[(3,1)、(9,1)],'irure':[(1,1],'laboris':[(2,1)],'sit:[(1,1)],'sunt':[(4,1],'eu 3,1],'nisi':[(2,1)]
#对于Python 3.7,dict_sorted={}#+
dict_sorted=Collections.OrderedDict()#对于Python<3.7
对于排序中的元素(dict_unsorted.keys()):
sortedVal=已排序(dict_未排序[元素])
dict_sorted[元素]=sortedVal
打印(已分类)
正如@DeepSpace OP所指出的,它使用Python 3.6
因此,dicts order是一个实现细节,我们必须使用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)
输出:
{'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)]}
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)])])
不知道为什么要进行向下投票,这个问题有什么问题?我理解值的排序,但为什么键的顺序很重要?如果您试图输出“排序的JSON”,请记住,根据RFC,JSON格式是无序的。我正在构建一个信息检索引擎。这将是一个反向索引,因此字典应该首先按术语排序,然后针对每个术语排序,它出现在哪个文档中(元组是document_id,#u出现次数)。这是构建该数据结构的一个要求。OP使用Python 3.6,其中dicts order是一个实现细节,因此您必须使用
orderedict
OP使用Python 3.6,其中dicts order是一个实现细节,因此您必须使用orderedict
Wait,但在Python 3中,顺序是从3.6开始的右@DeepSpace?开始执行的。6这是一个实现细节。在>=3.7中,这是有保证的。哦,好了,很高兴知道谢谢@DeepSpace will updateOP使用Python 3.6,其中dicts order是一个实现细节,所以您必须使用OrderedDict