I'm having trouble splicing one list into another at a given index. The idea is as such:
"For each word in the primary list, if that word is X, replace it with all the words in the secondary list."
Here's an example. It inserts the secondary list into the primary list at the correct index:
primary_list = ['the', 'black', 'cat', 'jumped']
secondary_list = ['big', 'grey']
for i, word in enumerate(primary_list):
if word == "black":
primary_list[i] = secondary_list
primary_list
>> ['the', ['big', 'grey'], 'cat', 'jumped']
The ideal output is:
['the', 'big', 'grey', 'cat', 'jumped']
I have also tried list comprehension:
primary_list[i] = [item for item in secondary_list]
Not only is it redundant, but it also just does the same as above.
There are some other answers here that used list slicing, but I wasn't sure how to use them in my syntax. Thoughts?
Here's an option using an intermediate result
list:
>>> primary_list = ['the', 'black', 'cat', 'jumped']
>>> secondary_list = ['big', 'grey']
>>> result = []
>>> for word in primary_list:
... if word == "black":
... result += secondary_list
... else:
... result.append(word)
...
>>> result
['the', 'big', 'grey', 'cat', 'jumped']
The problem in your code was because of two reasons in the following line:
primary_list
.secondary_list.copy()
. If you had multiple occurrences of 'black'
, then this would copy
the secondary list there. Any one of which if changed, it would later on change the others as they would share the same memory location.
primary_list[i] = secondary_list
Here is the solution.
final_list = list()
for word in primary_list:
if word=='black':
final_list += secondary_list.copy()
else:
final_list.append(word)
final_list
Output :
['the', 'big', 'grey', 'cat', 'jumped']
You should create another list and add primer list elements and secondary list elements.
primary_list = ['the', 'black', 'cat', 'jumped']
secondary_list = ['big', 'grey']
result_list=[]
for i, word in enumerate(primary_list):
if word == "black":
for secod_val in secondary_list:
result_list.append(second_val)
else:
result_list.append(word)
A simple approach using slicing.
>>> a = [1, 2, 3, 7, 8]
>>> b = [4, 5, 6]
>>> a[3:3] = b
>>> a
[1, 2, 3, 4, 5, 6, 7, 8]
primary_list = ['the', 'black', 'cat', 'jumped']
secondary_list = ['big', 'grey']
for i, word in enumerate(primary_list):
if word == "black":
primary_list[i:len(secondary_list)] = secondary_list
print(primary_list)
['the', 'big', 'grey', 'cat', 'jumped']
Another option using strings:
primary_list = ['the', 'black', 'cat', 'jumped']
secondary_list = ['big', 'grey']
for i in range(len(primary_list)):
if primary_list[i] == 'black':
primary_list[i] = secondary_list
final_list = repr(primary_list).replace('[', '').replace(']', '').replace("'", '').split(', ')
print(final_list)
Output:
['the', 'big', 'grey', 'cat', 'jumped']
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.