I have a list of alphanumeric strings:
list = ["abc 123", "456 jkl"]
I also have a dictionary that contains substrings of the strings in the list above as keys:
dict = {"abc":"xyz", "jkl":"stu"}
I want to update the list using the dictionary so that the result looks like:
result = ["xyz 123", "456 stu"]
Basically, I want to replace any component (and only that component) in the list that matches dictionary keys with dictionary values.
I tried iterating through the dictionary + the list to do so, but I am having trouble updating just the substring. I would also like to learn more efficient/pythonic way of achieving this, please.
for element in list:
for key,value in dictionary.items():
if key in element:
element = value
You might get the result with list comprehension and replace()
method like below -
l = ["abc 123", "456 jkl"]
d = {"abc":"xyz", "jkl":"stu"}
l = [e.replace(key, val) for e in l for key, val in d.items() if key in e]
print(l)
Simple loop will do as well.
But remember in this solution if any key of the dictionary present as part of the word that will be replaced as well. If you don't want that then you can split the elements of the list first to get the result. Or you can use regex to do that
is like this? you must create a new list (based on your code)
list = ["abc 123", "456 jkl"]
dict = {"abc":"xyz", "jkl":"stu"}
newDict = []
for element in list:
for key,value in dict.items():
if key in element:
newElement = element.replace(key, value)
newDict.append(newElement)
print newDict
If you are prepared to use regex
:
>>> import re
>>> result = re.sub(
r'\b'+r'|\b'.join(dct)+r'\b',
lambda m: dct.get(m.group(), m.group()),
','.join(lst)
).split(',')
# or
>>> result = [re.sub(
r'\b'+r'|\b'.join(dct)+r'\b',
lambda m: dct.get(m.group(), m.group()),
item
) for item in lst]
>>> result
["xyz 123", "456 stu"]
Where,
r'\b'+r'|\b'.join(dct)+r'\b'
joins keys of dct
with delimter |
to form the pattern string.
lambda m: dct.get(m.group(), m.group())
creates a callable, that, if match found, returns value for that matching key from dct
else returns the match as is.
','.join(lst)
and .split(',')
is a way to do this without a loop, only if your strings do not contain comma, otherwise some other delimiter can be used.
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.