有人可以告诉我如何对这个进行排序:
{'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']}
转换为
{'a': [1, 2, 3], 'b': ['blah', 'bhasdf', 'asdf'], 'c': ['one', 'two'],'d': ['asdf', 'wer', 'asdf', 'zxcv']}
谢谢!
更新1,代码示例:
因此,我正在进行语言学研究。一篇文章被分解为单词并存储在数据库中,其中包括段落ID和句子ID等所有属性。任务是:尝试重建原始文本。
从数据库获取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]=[]
然而,在这一点上,如果我尝试循环字典并重建文本,一些较晚的带有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 %}
.belongs_to_paragraph
不是内置类型,而是其他东西。它是什么类型的对象?你能从中得到可排序的东西吗?比如pp =w.belongs_to_paragraph.value
或者其他什么? - Lennart Regebro