Ad

How To Remove The First And Last Value Of The First And Last Element That Is Separated By (.) And Need To Join The Other Elements In The List

- 1 answer

I have a python-list like this

['6403687.6403773','6404555.6404614','6413270.6413335']

In this I need to remove first value of the first element(6403687) and last value of the last element(6413335) and need to join the other element like this ['6403773.6404555','6404614.6413270'].Like this I have several list with n values.I don't how to do this.If anyone please help me.

list = ['6403687.6403773','6404555.6404614','6413270.6413335']

After removing the first and last values,I need a list like this

list1 = ['6403773.6404555','6404614.6413270']
Ad

Answer

This method would do it step by step and works also for lists or arbitrary length:

number_strings = ['6403687.6403773','6404555.6404614','6413270.6413335']

# remove first part of element
number_strings[0] = number_strings[0].split('.')[1]

# remove last part of last element
number_strings[-1] = number_strings[-1].split('.')[0]

# remove points
number_strings_rearranged = []
for element in number_strings:
    for part_string in element.split('.'):
        number_strings_rearranged.append(part_string)

# restructure with points
number_strings = [number_strings_rearranged[i]+'.'+number_strings_rearranged[i+1] for i in range(0, len(number_strings_rearranged)-1, 2)]

print(number_strings)

Output:

['6403773.6404555', '6404614.6413270']
Ad
source: stackoverflow.com
Ad