I have a list of strings - List A
['red fruit', 'orange', 'banana', 'fruit with potassium']
I want to find each of the strings in list A in a list of lists (List B):
[['apple', 'red fruit'], ['banana', 'fruit with potassium'], ['orange', 'citrus fruit']]
and replace it with a corresponding value (from List C) for the identified list in List B
List C - ['apple', 'banana', 'orange']
The final replaced list (answer) for List A should be:
['apple', 'orange', 'banana', 'banana']
It would also be better if List B and List C are stored in a better data structure.
I am unable to find a way to write this programmatically.
CodePudding user response:
You can use List Comprehension, and iterate the lists and check for the conditions i.e. common between first and second list and second and third list:
>>> [c for a in list_a for b in list_b for c in list_c if a in b and c in b]
['apple', 'orange', 'banana', 'banana']
PS: The sole purpose of list-comprehension is to make the code clean, and more readable. However, for this type of nested iterations, I'd actually prefer to iterate the values in traditional loop to make the code more readable.
CodePudding user response:
You can use this code:
a = ['red fruit', 'orange', 'banana', 'fruit with potassium']
b = [['apple', 'red fruit'], ['banana', 'fruit with potassium'], ['orange', 'citrus fruit']]
c = a[:]
for item in b:
for j,k in enumerate(a):
if item[1] == k:
a[j] = item[0]
a,c = c,a
print(a)
print(c)
CodePudding user response:
I would make list B into a dict
that maps from the word-to-be-replaced to its replacement:
list_a = ['red fruit', 'orange', 'banana', 'fruit with potassium']
list_b = [['apple', 'red fruit'], ['banana', 'fruit with potassium'], ['orange', 'citrus fruit']]
# Build dict/map out of list_b
replacement_map = {}
for (value, key) in list_b:
replacement_map[key] = value
# Build list_c
list_c = []
for item in list_a:
if item in replacement_map:
list_c.append(replacement_map[item])
else:
list_c.append(item)
CodePudding user response:
Try this with dicts for 1 vs 1 situation:
list_ini = ['red fruit', 'orange', 'banana', 'fruit with potassium']
list_match = [['apple', 'red fruit'], ['banana', 'fruit with potassium'], ['orange', 'citrus fruit']]
list_fruits = ['apple', 'banana', 'orange']
dict_fruits = {}
for i in list_match:
dict_fruits[i[1]] = i[0]
# print(dict_fruits)
list_rep = [dict_fruits[x] if x in dict_fruits else x for x in list_ini]
print(list_rep)
Try this with dicts for N vs 1 situation if I understand the comment correctly:
list_ini = ['red fruit', 'orange', 'banana', 'fruit with potassium', 'yellow fruit', 'citrus fruit', 'orange fruit', 'VitaminC']
list_match = [['apple', 'red fruit'], ['banana', 'fruit with potassium', 'yellow fruit'], ['orange', 'citrus fruit', 'orange fruit', 'VitaminC']]
list_fruits = ['apple', 'banana', 'orange']
list_new = []
for x, char in enumerate(list_ini):
for i in range(len(list_match)):
for j in range(len(list_match[i])):
if list_match[i][j] == char:
print(f'{char} is a description of {list_match[i][0]}')
# print(f"Index of 'char': {i}")
# print(list_match[i][0])
fruit_rep = list_match[i][0]
list_new.append(fruit_rep)
print(list_new)