How to make a zipped OrderedDict with multiple lists based on a sentence?

236 Views Asked by At

I have six list, three of them correspond to the given sentence and the other three lists correspond to the labels of the previous lists. I want to zip these lists into an OrderedDict, based on the order of the sentence.

from collections import OrderedDict

sentence = 'Im fucking excited to learn your damn business'

words = ['excited', 'learn', 'business']
stopwords = ['Im', 'to', 'about', 'your']
swearwords = ['fucking', 'damn']

# Labels
words_labels = ['friendly', 'interested', 'interested']
stopwords_labels = ['stopword'] * len(stopwords)
swearwords_labels = ['sweaword'] * len(swearwords)

d = OrderedDict(zip(keys, values))

# Expected output
{
    'Im': 'stopword',
    'fucking': 'swearword',
    'excited': 'friendly',
    'to': 'stopword',
    'learn': 'interested',
    'your': 'stopword'
    'damn': 'swearword',
    'business': 'interested'
}
1

There are 1 best solutions below

2
On BEST ANSWER

First to make a list of repeating content, the syntax is

stopwords_labels = ['stopword'] * len(stopwords)

Then build lists, of all content and all labels, then you were good

keys = [*words, *stopwords, *swearwords]
values = [*words_labels, *stopwords_labels, *swearwords_labels]
mappings = dict(zip(keys, values))
d = OrderedDict((k, mappings[k]) for k in sentence.split())

You can also use that syntax, that concatenates

keys = words + stopwords + swearwords
values = words_labels + stopwords_labels + swearwords_labels