mgP*_*ePe 18 python sorting dictionary
任何人都可以告诉我如何排序这个:
{'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']}
Run Code Online (Sandbox Code Playgroud)
成
{'a': [1, 2, 3], 'b': ['blah', 'bhasdf', 'asdf'], 'c': ['one', 'two'],'d': ['asdf', 'wer', 'asdf', 'zxcv']}
Run Code Online (Sandbox Code Playgroud)
?谢谢!
更新1,代码示例:
所以我在做语言学.一篇文章分解为存储在数据库中的单词,并具有各种属性,包括段ID和句子ID.任务:尝试重建原始文本.
从DB中获取500个连续的单词
words = Words.objects.all()[wordId:wordId+500]
# I first create paragraphs, through which I can loop later in my django template,
# and in each para will be a list of words (also dictionaries).
# So i am trying to get a dictionary with values that are lists of dictionaries.
# 'pp' i make just for shorthanding a long-named variable.
paras={}
para_high = para_low = words[0].belongs_to_paragraph
for w in words:
last_word = w
pp = w.belongs_to_paragraph
if pp >para_high:
para_high = pp
if pp < para_low:
para_low = pp
if pp in paras:
paras[pp].append(w)
else:
list = [w]
paras[pp] = list
# Since there are blank lines between paragraphs, in rebuilding the text as it
# looked originally, I need to insert blank lines.
# Since i have the ID's of the paragraphs and they go somewhat like that: 1,3,4,8,9
#(the gaps between 1 & 3 and 4 & 8 i have to fill in with something else,
# which is why i had para_low and para_high to loop the range.
isbr = True
for i in range(para_low, para_high+1):
if i in paras:
isbr = True
else:
if isbr:
paras[i]=['break']
isbr = False
else:
paras[i]=[]
Run Code Online (Sandbox Code Playgroud)
但是,在这一点上,如果我尝试循环dict并重建文本,一些后来的id段落在之前的段落之前,那就不会这样做.
更新2,循环代码:
{% for k,v in wording.iteritems() %}
{% if v[0] == 'break' %}
<br/>
{% else %}
</div><div class="p">{% for word in v %}{% if word.special==0%} {% endif %}<span class="word {% if word.special == 0%}clickable{% endif%}" wid="{{word.id}}" special="{{word.special}}" somethingElse={{word.somethingElse}}>{{ word.word }}</span>{% endfor %}
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 46
Dicts没有订单.
你可以调用sorted,但这只是给你一个键的排序列表:
>>> sorted(d)
['a', 'b', 'c', 'd']
Run Code Online (Sandbox Code Playgroud)
您可以将其视为可迭代的并对键值元组进行排序,但之后您就会得到一个元组列表.这和dict不一样.
>>> sorted(d.items())
[
('a', [1, 2, 3]),
('b', ['blah', 'bhasdf', 'asdf']),
('c', ['one', 'two']),
('d', ['asdf', 'wer', 'asdf', 'zxcv'])
]
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Python 2.7或更高版本,您也可以考虑使用OrderedDict
.
记住订单条目的dict子类已添加
例如:
>>> d = collections.OrderedDict(sorted(d.items()))
>>> for k, v in d.items():
>>> print k, v
Run Code Online (Sandbox Code Playgroud)
a [1, 2, 3] b ['blah', 'bhasdf', 'asdf'] c ['one', 'two'] d ['asdf', 'wer', 'asdf', 'zxcv']
Len*_*bro 29
在正确的回答是,如果你想在一个有序字典的项目,你应该使用排序()函数,当你遍历所有的字典:
for k, v in sorted(d.items()):
print k, ':', v
Run Code Online (Sandbox Code Playgroud)
要么
for k in sorted(d):
print d[k]
Run Code Online (Sandbox Code Playgroud)
或类似的.
提到的OrderedDict适用于有订单的字典.订单与排序不一样.您可以创建一个已排序的OrderedDict,但是,只要添加新密钥,就不再对其进行排序.所以你需要在每次使用之前或每次操作之后使用sorted()进行排序.因此,OrderedDict只比普通字典更慢,占用内存更多,同时不需要添加任何内容.
OrderedDict 不适用于已排序的词典,但适用于字典,其中项目具有某种不是排序的排序.例如,如果您想按照添加顺序显示内容,或者您希望用户能够任意订购内容.
更新:进一步说明
为什么OrderedDict不是解决方案?因为订购的OrderedDict 没有排序.
考虑一个标准字典:
>>> d = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5}
Run Code Online (Sandbox Code Playgroud)
它没有排序,如下所示,'c'将在'b'之前出现.它也没有订单,如果我们添加新的东西,它看起来像是随机顺序:
>>> d['g'] = 6
>>> d['i'] = 8
>>> d
{'a': 0, 'c': 2, 'b': 1, 'e': 4, 'd': 3, 'g': 6, 'f': 5, 'i': 8}
Run Code Online (Sandbox Code Playgroud)
好的,那么让我们使用OrderedDict:
>>> o = OrderedDict(sorted({'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5}.items()))
>>> o
OrderedDict([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4), ('f', 5)])
Run Code Online (Sandbox Code Playgroud)
啊哈!排序!所以OrderedDict有效!?没有.
>>> o['i'] = 8
>>> o['g'] = 6
>>> o
OrderedDict([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4), ('f', 5), ('i', 8), ('g', 6)])
Run Code Online (Sandbox Code Playgroud)
什么?在我之后,g结束了吗?!?为什么!?因为OrderedDict没有排序,所以它是有序的.它会记住你添加东西的顺序.不是排序.这意味着每次使用它时都需要先对其进行排序.只要您不向其添加密钥,OrderedDict将仅保持排序.但如果你不打算修改它,那么你就不需要一个字典.你也可以有一个清单.这是你从sorted()得到的:
>>> sorted(o.items())
[('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4), ('f', 5), ('g', 6), ('i', 8)]
Run Code Online (Sandbox Code Playgroud)
但是这与标准字典一样有效,所以OrderedDictionary没有帮助:
>>> sorted(d.items())
[('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4), ('f', 5), ('g', 6), ('i', 8)]
Run Code Online (Sandbox Code Playgroud)
结束语 因此,每次要以排序方式遍历字典时,都需要执行以下操作:
>>> for k in sorted(o):
... print k, o[k]
...
a 0
b 1
c 2
d 3
e 4
f 5
g 6
i 8
Run Code Online (Sandbox Code Playgroud)
这就是你使用的字典.OrderedDict并没有真正帮助你,因为它不关心排序,只关心你添加内容的顺序.
值得注意的是,Python有许多字典实现,可以按排序顺序维护密钥.考虑sortedcontainers模块,它是纯Python和快速实现的C实现.与其他快速和功能完备的实现进行了性能比较.
例如:
>>> from sortedcontainers import SortedDict
>>> d = {'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']}
>>> s = SortedDict(**d)
>>> s.keys()
SortedSet(['a', 'b', 'c', 'd'])
Run Code Online (Sandbox Code Playgroud)
您还可以完全用SortedDict替换您对dict的使用,因为它支持快速获取/设置操作以及按键排序的项目迭代.