简体   繁体   中英

Store and update values in dict on iterating on python

What is the best way in this case, to store for every Speaker the spoken text in a form of a dict or a better option? I want to map every spoken text to each speaker like this try. But the output is not as I expected it.

def speaker_texts(cleanedList):

    dictspeaker = {"Speaker": "", "Group": "", "Text": ""}

    pattern_speaker = r"([A-Z]+[a-z]*)([\s]*)(\([A-Z]*\))"

    for sent in cleanedList:
        speaker = re.findall(pattern_speaker, sent)

        for info in speaker:

            dictspeaker.update({"Speaker":info[0], "Group":info[2], "Text": sent})

Output:

{'Speaker': 'Rische', 'Group': '(KPD)', 'Text': ', Antragsteller: Meine Damen und \nHerren! Anläßlich der Regierungserklärung und \n\n\x0c\n\n30 \n\n(Rische) \nauch in der heutigen Debatte zum Flüchtlings-\nproblem wurden viele Worte über eine sinnvolle, \nden sozialen Belangen entsprechende Verwendung \nöffentlicher Mittel gesprochen. Di e Regierung gab \nin ihrem Programm zu verstehen, daß sie eine ver-\nantwortungsbewußte  Sozialpolitik  durchzuführen \ngedenke. Sie hat die Flüchtlingshilfe, den Woh-\nnungsbau, die Verbe.'}

In the file a speaker comes forward several times. I would like to assign the spoken texts to the respective speaker. That is, whenever a speaker occurs, update it in the dictionary so that the new text is added without overwriting the old one.

Or should I create for every Speaker a own dict?

You may want to use namedtuple and create your speaker objects. then append them to a list. Here I write some code to show you how to use nametuple.

In [1]: from collections import namedtuple

In [2]: speaker = namedtuple('Speaker', 'speaker goroup text')

In [3]: speaker1 = speaker('Rische', 'KPD', "Antragsteller: Meine 
Damen")

In [4]: speaker1
Out[4]: Speaker(speaker='Rische', goroup='KPD', text='Antragsteller: 
Meine Damen')

In [5]: speaker1.text
Out[5]: 'Antragsteller: Meine Damen'

In nametuple you just define your field names( speaker, group, text). These fields could be anything like dictionaries.

In [19]: speaker = namedtuple('Speaker', 'speaker goroup text')

In [20]: text = {'a':1, 'b':2}

In [21]: speaker1 = speaker('Rische','KPD',text)

In [22]: speaker1
Out[22]: Speaker(speaker='Rische', goroup='KPD', text={'b': 2, 'a': 1})

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