With a string you can replace substrings of length greater than 1. For example, 'abcabc'.replace('abca','*')
yields '*bc'
.
I would like to do this with a list. For example, something like this:
[1, [0], 'a', 1, [0], 'a'].replace([1, [0], 'a', 1], [5])
should yield
[5, [0], 'a']
Note that this question is not a duplicate because they do not need to match patterns but only specific items of the list.
a solution that works, replaces the sub-list in-place by using slice assignment:
def replace_list(lst,sublst,replacement):
lensub = len(sublst)
i = 0
while i <= len(lst)-lensub:
if lst[i:i+lensub] == sublst:
lst[i:i+lensub] = replacement
i += len(replacement)
else:
i += 1
lst = [1, [0], 'a', 1, [0], 'a']
replace_list(lst,[1, [0], 'a', 1], [5])
now lst
is:
[5, [0], 'a']
more complex input (to test end condition & multiple replacements)
lst = [1, [0], 'a', 1, 1, [0], 'a', 1, [0], 'a',1, [0], 'a', 1]
yields once replaced:
[5, 5, [0], 'a', 5]
how it works:
I'm not too happy about the constant list slicing, but it's needed here to perform equality with the other list, and creating an inner loop would be more cumbersome and not necessarily faster.
If you don't want to work in-place, you could create a copy at start,work on the copy and return it:
def replace_list(lst_,sublst,replacement):
lst = lst_.copy()
...
return lst
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With