Reputation: 45
I want to replace strings in a list with values from dictionary. However, for some logical reason which is not logical to me (obviously), length of list changes after replacement.
genre_list = ['action, drama, thriller', 'crime, romance, adventure']
list_new = []
categories_ids = {'action': '18',
'drama': '13',
'thriller': '11',
'romance': '1',
'adventure': '8',
'crime': '3'
}
print(len(genre_list)) # length before
for z in genre_list:
for a, b in categories_ids.items():
if a in z:
list_temp = z.replace(z, b)
list_new.append(list_temp)
print(len(list_new)) # length after
What am I missing here? Thanks in advance.
Upvotes: 1
Views: 103
Reputation: 44
Each list element contains more than 1 keys which is reason you end up with more elements in the new list. This can be handled as given the code below.
for z in genre_list:
key_words=''
for key in z.split(','):
if key.strip() in categories_ids:
key_words += categories_ids[key.strip()] +','
list_new.append(key_words[:-1])
Now both the lists will have same length as given below.
2 ['action, drama, thriller', 'crime, romance, adventure']
2 ['18,13,11', '3,1,8']
Upvotes: 1
Reputation: 17322
you are adding new elements to your new_list
if a key from your dict is in one string from genre_list
but the genre_list
has in one string multiple keys from your dict so you end to have multiple strings/elements in your new_list
you can use a regular expression with list comprehension:
import re
genre_list = ['action, drama, thriller', 'crime, romance, adventure']
pattern = '|'.join(categories_ids)
def replace(gr):
return categories_ids[gr.group()]
list_new = [re.sub(pattern, replace, t) for t in genre_list]
# ['18, 13, 11', '3, 1, 8']
Upvotes: 1
Reputation: 9494
You append to list_new
each element from categories
that appears in each element of genre_list
- the first 3 keys appears in the first element of genre_list
and the 3 other keys appears in the second element of genre_list
- so in list_new
will be 6 elements in total.
Try instead:
genre_list = ['action, drama, thriller', 'crime, romance, adventure']
list_new = []
categories_ids = {'action': '18',
'drama': '13',
'thriller': '11',
'romance': '1',
'adventure': '8',
'crime': '3'
}
for z in genre_list:
for a, b in categories_ids.items():
z = z.replace(a, b)
list_new.append(z) # here is the difference - one append per element in genre_list
print(list_new) # output:['18, 13, 11', '3, 1, 8']
Upvotes: 2
Reputation: 71689
Use:
def func(s):
return ", ".join(categories_ids[w] for w in s.split(", "))
list_new = list(map(func, genre_list))
print(list_new)
This prints:
['18, 13, 11', '3, 1, 8']
Upvotes: 1