Can anyone tell me how I can sort this:
{'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']}
into
{'a': [1, 2, 3], 'b': ['blah', 'bhasdf', 'asdf'], 'c': ['one', 'two'],'d': ['asdf', 'wer', 'asdf', 'zxcv']}
?
Thanks!
UPDATE 1, code sample:
So I am doing linguistics. One article is broken down to words that are stored in a database and have all kinds of properties including para ID and sentence ID. The task: trying to rebuild the original text.
Get 500 consecutive words from DB
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]=[]
At this point, however, if I try to loop the dict and rebuild the text, some later id'd paragraphs come before previous ones, and that just doesn't do it.
UPDATE 2, loop code:
{% 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 %}
See Question&Answers more detail:
os