Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (19.9k points)

I want to simplify this construction with list comprehensions:

words = {}

counter = 0

for sentence in text:

    for word in sentence:

        if word not in words:

            words[word] = counter

            counter += 1

If there was something like post-increment, it could be written like:

words = {word: counter++ for sentence in text for word in sentence if word not in words}

How should I do it in pythonic way?

For example:

text =

[

['aaa', 'bbb', 'ccc'],

['bbb', 'ddd'],

['aaa', 'ccc', 'eee']

]

Desired result:

words = {'aaa': 1, 'bbb': 2, 'ccc': 3, 'ddd': 4, 'eee': 5}

Order does not matter.

UPD:

I found an interesting solution:

words = {}

counter = (x for x in range(10**6))

[words.update({word: counter.next()}) for sentence in text for word in sentence if word not in words]

update method allows to check if word in dictionary already. Maybe I should use len(words) instead of counter.next(), but I thought that counter will be faster (O(1) vs. O(dict_size)).

1 Answer

0 votes
by (25.1k points)

Use a dictionary, then you should use its setdefault method, it makes this kind of tasks trivial.

words = {}

for sentence in text:

    for word in sentence:

        words[word] = words.setdefault(word, 0) + 1

Browse Categories

...