Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How split an element and replace both part in list with python

I've a list like:

mylist = ['La', 'domestication', "d'un", 'animal', 'ou', "d'un", 'végétal,', 'necessite', "l'acquisition", "d'une", 'ferme']

I want to split elements which have " ' " inside by 2 elements and keep their index in the original list.

OUTPUT REQUESTED : my_new_list = ['La', 'domestication', "d'" ,'un', 'animal', 'ou', "d'", 'un', 'végétal,', 'necessite', "l'", 'acquisition', "d'", 'une', 'ferme']

I've tried few thing but I admit I'm stuck to replace the two new split element in the correct index, here is the code I've tried:

for word in mylist:
    if "'" in word:
        new_words = word.split("'")
        mylist[mylist.index(word)] = (new_words[0]+"'")
        mylist.insert(mylist.index((new_words[0]+"'")+1), new_words[1]) 

print(mylist)

Thank you for your time and help :)

like image 225
Arthur Avatar asked Jun 30 '26 23:06

Arthur


1 Answers

Assuming you're happy with creating a new list, one way to achieve that is to join the existing list together with spaces and then split on either a space or a ':

import re

mylist = [
 'La', 'domestication', "d'un",
 'animal', 'ou', "d'un", 'végétal,',
 'necessite', "l'acquisition",
 "d'une", 'ferme'
]

my_new_list = re.split(r" |(?<=')", ' '.join(mylist))

Output:

[
 'La', 'domestication', "d'", 'un',
 'animal', 'ou', "d'", 'un', 'végétal,',
 'necessite', "l'", 'acquisition',
 "d'", 'une', 'ferme'
]

Note this assumes the words in the list don't have a space in them; if they might, you can just replace the space in the code with a character (or character sequence) which does not occur in the words, e.g. \0:

my_new_list = re.split(r"\0|(?<=')", '\0'.join(mylist))
like image 74
Nick Avatar answered Jul 03 '26 12:07

Nick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!