0

I'm newbie to python. I have this program:

wordlist = ['pea', 'rpai', 'rpai', 'schiai', 'pea', 'rpe', 'zoi', 'zoi', 'briai', 'rpe']
dictionary = {}
counter = 0
result = list(map(lambda x: dictionary[wordlist[x]] = dictionary.get(wordlist[x], counter +=1), wordlist))
print(result)

Result has to be:

result = [0, 1, 1, 2, 0, 3, 4, 4, 5, 3]

What I have to do is append all of the element in the list (as key) inside the dictionary with an incremental counter as value of the key. With this code I get "lambda cannot contain assignment. How can I do this? Thank you!

EDIT FOR EXPLANATION:

With the list of strings I have to create a dictionary with element of list of str as "argument" and value as "key"

The value is calculated like this: The first element of the list is 0. The following element, if it is a new string never appeared (unique) has last value (in this case 0) =+1. Instead if the new element is a duplicate string (there is already one in the dictionary) it take the same originale value as the first.

The dictionary will be:

{'pea': 0, 'rpai': 1, 'rpai': 1, 'schiai': 2, 'pea': 0, 'rpe': 3, 
 'zoi': 4, 'zoi': 4, 'briai': 5,'rpe': 3}

And result instead with list will be:

[0, 1, 1, 2, 0, 3, 4, 4, 5, 3]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Alessio
  • 35
  • 5

4 Answers4

2

I guess the easiest solution with vanilla Python is to use defaultdict:

from collections import defaultdict

wordlist = ["pea","rpai","rpai","schiai","pea","rpe", "zoi","zoi","briai","rpe"]
vocab = defaultdict(lambda: len(vocab))
# result will be [0, 1, 1, 2, 0, 3, 4, 4, 5, 3]
result = [vocab[word] for word in wordlist]

A more verbose equivalent, leading to the same result:

vocab = {}
result = []
for word in wordlist:
    if word not in vocab:
        vocab[word] = len(vocab)
    result.append(vocab[word])
Marat
  • 15,215
  • 2
  • 39
  • 48
1

Update: Use dictionary's setdefault then.

wordlist = ["pea","rpai","rpai","schiai","pea","rpe", "zoi","zoi","briai","rpe"]
dic = {}
res = list(map(lambda x: dic.setdefault(x, len(dic)), wordlist))
print(res)

Dictonary can't have same keys. You just need a for loop:

wordlist = ["pea","rpai","rpai","schiai","pea","rpe", "zoi","zoi","briai","rpe"]
c = 0
dic = {}
res = []
for i in range(len(wordlist)):
    word = wordlist[i]
    if word in dic:
        res.append(dic[word])
    else:
        dic[word] = c
        res.append(c)
        c += 1

print(res)
Frank
  • 1,215
  • 12
  • 24
1

Once you have the dictionary built the code for the lambda will be as follows.

list(map(lambda x: dictionary[x], wordlist))

This assumes you already have the keys and values of the dictionary populated. Is this the case, like so?

{'pea': 0, 'rpai': 1, 'schiai': 2, 'rpe': 3, 'zoi': 4, 'briai': 5, 'rpei': 6}
Stu
  • 421
  • 3
  • 7
  • It's much cleaner as a comprehension: `[dictionary[x] for x in wordlist]` – wjandrea Nov 07 '20 at 23:01
  • @wjandrea I know, but the OP has stated that the solution must use map and lambda – Stu Nov 07 '20 at 23:02
  • Sorry , I made a mistake with the request and the result is a little bit different, what i have to do, create a new post (and delete this) or append the edit? Thank you – Alessio Nov 08 '20 at 21:53
1

All you need to do is make a dict with the first occurrence of each word, then look up each word in the dict. Don't use map and lambda, they'll only make it harder, or at least less readable.

first_occ = {}
counter = 0
for word in wordlist:
    if word not in first_occ:
        first_occ[word] = counter
        counter += 1

result = [first_occ[w] for w in wordlist]
print(result)  # -> [0, 1, 1, 2, 0, 3, 4, 4, 5, 3]
wjandrea
  • 28,235
  • 9
  • 60
  • 81