Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a sublist with another sublist in python

Tags:

python

list

I want to replace a sub-list from list a, with another sub-list. Something like this:

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

Lets say I want to take a sublist like:

a_sub=[3,5,10]

and replace it with

b_sub=[9,7]

so the final result will be

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

Any suggestions?

like image 457
darxyde Avatar asked Oct 15 '12 14:10

darxyde


People also ask

How do you replace an item in a list with another list in Python?

The easiest way to replace an item in a list is to use the Python indexing syntax. Indexing allows you to choose an element or range of elements in a list. With the assignment operator, you can change a value at a given position in a list.

How do you append to a sublist in Python?

To add new values to the end of the nested list, use append() method. When you want to insert an item at a specific position in a nested list, use insert() method. You can merge one list into another by using extend() method.


3 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

like image 136
inspectorG4dget Avatar answered Oct 16 '22 01:10

inspectorG4dget


You can do this nicely with list slicing:

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

So how do we get the indices here? Well, let's start by finding the first one. We scan item by item until we find a matching sublist, and return the start and end of that 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

We can now do our replacement - we start at the beginning, replace the first one we find, and then try to find another after our newly finished replacement. We repeat this until we can no longer find sublists to replace.

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

Which we can use nicely:

>>> a=[1, 3, 5, 10, 13]
>>> replace_sublist(a, [3, 5, 10], [9, 7])
>>> a
[1, 9, 7, 13]
like image 28
Gareth Latty Avatar answered Oct 16 '22 01:10

Gareth Latty


You need to take a slice from start_index to end_index + 1, and assign your sublist to it.

Just like you can do: - a[0] = 5, you can similarly assign a sublist to your slice: - a[0:5] -> Creates a slice from index 0 to index 4

All you need is to find out the position of the sublist you want to substitute.

>>> 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 that, substituted sublist don't have to be of same length of the substituting sublist.

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

like image 42
Rohit Jain Avatar answered Oct 16 '22 03:10

Rohit Jain