Home > database >  How to change the python string substring information
How to change the python string substring information

Time:10-18

I have 1 bitsInfo string:

bitsInfo="0100001111110001"

and 1 array bitReplace which includes subarray:

bitReplace=[["1","5","00000"],["8","11","0000"]]

The first element of the subarray is startbit location and the second element is the endbit location.

The goal of the script is to replace the bitsInfo string (with the third element of subarray) base on startbit and endbit information.

The expected result should be

bitsFinal="0000001100000001"

I have tried this method:

for bits in bitReplace:
    bitsFinal = bits[:int(bits[0]) bits[2]  bits[int(bits[1] 1:]

This method doesn't really work. May I know what went wrong?

CodePudding user response:

You are close but you are not using the original string anywhere. Try this:

bitsFinal = bitsInfo
for bits in bitReplace:
     bitsFinal = bitsFinal[:int(bits[0])]   bits[2]   bitsFinal[int(bits[1]) 1:]

the result is:

>>> bitsFinal
'0000001100000001'

CodePudding user response:

for bits in bitReplace:
  bitsFinal = bits[:int(bits[0])] bits[2]  bits[int(bits[1]) 1:]

I think there are problems with parantheses.

  • Related