Sorting dict items by key, beyond alphanumeric sorting
需要以这种方式订购类似元组的对象https://stackoverflow.com/users/2141635/padraic-cunningham我很好地回答了我,
现在我需要修改元组项目,例如:
od = sorted(dizN.items(), key=key_func) #creates the sorted list of tuples
od[3][1]+=1 #is my attempt to increase by 1 the 2nd element of the 3rd tuple in the list of tuples; of course it is not possible.
请记住哪种对象是“od”:
od
Out[157]:
[('a0p12', 0),
('a1p11', 0),
('a2p10', 0),
('a3p9', 0),
('a4p8', 0),
('a5p7', 0),
('a6p6', 0),
('a7p5', 0),
('a8p4', 0),
('a9p3', 0),
('a10p2', 0),
('a11p1', 0),
('a12p0', 0)]
我认为这个问题的一个很好的解决方案是将这个元组列表转换为2个元素列表的列表; 这是我想要的输出:
[['a0p12', 0],
['a1p11', 0],
['a2p10', 0],
['a3p9', 0],
['a4p8', 0],
['a5p7', 0],
['a6p6', 0],
['a7p5', 0],
['a8p4', 0],
['a9p3', 0],
['a10p2', 0],
['a11p1', 0],
['a12p0', 0]]
有人知道怎么做吗? 非常感谢任何人都会回答!