1 回答
TA贡献1779条经验 获得超6个赞
您正在覆盖每个循环的值。在循环外声明列表。
前任:
context['lyrics_lemma'] = []
context['count'] = []
for song in context['song_list']:
song = models.Song.objects.get(title=song)
lyrics_list = models.Song.objects.get(title=song).lyrics_as_list()
import pymorphy2
morph = pymorphy2.MorphAnalyzer()
lyrics_list_lemma = []
for word in set(lyrics_list):
parsed_word = morph.parse(word)[0]
result = {
'word_original': word,
'word_normalized': parsed_word.normal_form,
}
lyrics_list_lemma.append(result)
context['lyrics_lemma'].append(lyrics_list_lemma)
context['count'].append(len([k for d in lyrics_list_lemma for k in d.keys() if k == 'word_normalized']))
return context
你也可能需要一本字典或字典列表
context['data'] = []
for song in context['song_list']:
.....
context['data'].append({"lyrics_lemma": lyrics_list_lemma,
"count": len([k for d in lyrics_list_lemma for k in d.keys() if k == 'word_normalized']))
或者
context['data'] = {}
for song in context['song_list']:
.....
context['data'].update({song: {"lyrics_lemma": lyrics_list_lemma,
"count": len([k for d in lyrics_list_lemma for k in d.keys() if k == 'word_normalized'])}})
添加回答
举报