Replacing subscriptions with another subscriber in python

I want to replace a sublist from list a , with another sublist. Something like that:

 a=[1,3,5,10,13] 

Suppose I want to take a dungeon like:

 a_sub=[3,5,10] 

and replace it with

 b_sub=[9,7] 

so the end result will be

 print(a) >>> [1,9,7,13] 

Any suggestions?

+8
python list
source share
4 answers
 In [39]: a=[1,3,5,10,13] In [40]: sub_list_start = 1 In [41]: sub_list_end = 3 In [42]: a[sub_list_start : sub_list_end+1] = [9,7] In [43]: a Out[43]: [1, 9, 7, 13] 

Hope that helps

+9
source share

You can do it nicely with a sliced ​​list:

 >>> a=[1, 3, 5, 10, 13] >>> a[1:4] = [9, 7] >>> a [1, 9, 7, 13] 

So how can we get indexes here? Well, let's start by finding the first one. We go through the item by item until we find the corresponding sublist, and return the beginning and end of this sublist.

 def find_first_sublist(seq, sublist, start=0): length = len(sublist) for index in range(start, len(seq)): if seq[index:index+length] == sublist: return index, index+length 

Now we can complete our replacement - we start from the very beginning, replace the first one we find, and then try to find another after our new replacement. We repeat this until we can find replacement subscriptions.

 def replace_sublist(seq, sublist, replacement): length = len(replacement) index = 0 for start, end in iter(lambda: find_first_sublist(seq, sublist, index), None): seq[start:end] = replacement index = start + length 

What we can use beautifully:

 >>> a=[1, 3, 5, 10, 13] >>> replace_sublist(a, [3, 5, 10], [9, 7]) >>> a [1, 9, 7, 13] 
+11
source share

You need to take a snippet from start_index to end_index + 1 and assign its subscriptions for it.

Just like you can do: - a[0] = 5 , you can also assign a sublist to your slice : - a[0:5] β†’ Creates a slice from index 0 to index 4

All you need to do is find the position sublist you want to replace.

 >>> a=[1,3,5,10,13] >>> b_sub = [9, 7] >>> a[1:4] = [9,7] # Substitute `slice` from 1 to 3 with the given list >>> a [1, 9, 7, 13] >>> 

As you can see, the substituted highlighter should not have the same length of substituting subscriptions.

In fact, you can replace 4 length lists with 2 length lists and vice versa.

+1
source share

Here is another way to do this. This method works if we need to replace multiple subscriptions:

 a=[1,3,5,10,13] a_sub=[3,5,10] b_sub=[9,7] def replace_sub(a, a_sub, b_sub): a_str = ',' + ','.join(map(str, a)) + ',' a_sub_str = ',' + ','.join(map(str, a_sub)) + ',' b_sub_str = ',' + ','.join(map(str, b_sub)) +',' replaced_str = a_str.replace(a_sub_str, b_sub_str)[1 : -1] return map(int, replaced_str.split(',')) 

Result:

 >>> replace_sub(a, a_sub, b_sub) [1, 9, 7, 13] >>> replace_sub([10, 13, 4], [3, 4], [7]) [10, 13, 4] #[3,4] is not in the list so nothing happens 

Replace a few subscriptions:

 >>> a=[1,3,5,10,13,3,5,10] >>> a_sub=[3,5,10] >>> b_sub=[9,7] >>> replace_sub(a, a_sub, b_sub) [1, 9, 7, 13, 9, 7] 
0
source share

All Articles