简体   繁体   中英

How to add values to a list inside a dic in python

I try to append strings into a list inside a dictionary, so that every participant would have a list of words. Here is my code:

words = [
    {'word': 'we', 'start_time': 90, 'participant': 'str_MIC_Y6E6_con_VnGhveZbaS'},
    {'word': "haven't", 'start_time': 91, 'participant': 'str_MIC_Y6E6_con_VnGhveZbaS'},
    {'word': 'even', 'start_time': 91, 'participant': 'str_MIC_Y6E6_con_VnGhveZbaS'},
    {'word': 'spoken', 'start_time': 91, 'participant': 'str_MIC_Y6E6_con_VnGhveZbaS'},
    {'word': 'about', 'start_time': 92, 'participant': 'str_MIC_Y6E6_con_VnGhveZbaS'},
    {'word': 'your', 'start_time': 92, 'participant': 'str_MIC_Y6E6_con_VnGhveZbaS'},
    {'word': 'newest', 'start_time': 92, 'participant': 'str_MIC_Y6E6_con_VnGhveZbaS'},
    {'word': 'some word here', 'start_time': 45, 'participant': 'other user'}
]

words.sort(key=lambda x: x['start_time'])

clean_transcript = []
wordChunk = {'participant': '', 'words': []}
for w in words:
    if wordChunk['participant'] == w['participant']:
        wordChunk['words'].append(w['word'])

    else:
        wordChunk['participant'] = w['participant']
        print(wordChunk['participant'])
        wordChunk['words'].append(w['word'])

clean_transcript.append(wordChunk)

This gives me this result:

[{'participant': 'str_MIC_Y6E6_con_VnGhveZbaS', 'words': ['some word here', 'we', "haven't", 'even', 'spoken', 'about', 'your', 'newest']}]

So the some word here is in a wrong list. How I need to modify this to create an own word list for other user also?

you can restucture your data some and just store it in a dict that has participants as keys:

wordChunk = {}
for w in words:
    wordChunk.setdefault(w["participant"],[]).append(w["word"])

wordChunk is now a dict with participants as keys:

>>> wordChunk
{'str_MIC_Y6E6_con_VnGhveZbaS': ['we', "haven't", 'even', 'spoken', 'about', 'your', 'newest'], 'other user': ['some word here']}

You can use itertools.groupby

from itertools import groupby

res = []
words = sorted(words, key = lambda x: x['start_time'])
for k, g in groupby(words, key = lambda x: x['participant']):
    d = {'participant': k, 'words': [x['word'] for x in g]} 
    res.append(d)
print(res)

Output:

[{'participant': 'other user', 'words': ['some word here']}, {'participant': 'str_MIC_Y6E6_con_VnGhveZbaS', 'words': ['we', "haven't", 'even', 'spoken', 'about', 'your', 'newest']}]

Using list comprehension

res = [{'participant': k, 'words': [x['word'] for x in g]} for k, g in
       groupby(sorted(words, key=lambda x: x['start_time']), key=lambda x: x['participant'])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM