Home > Software engineering >  How split an element and replace both part in list with python
How split an element and replace both part in list with python

Time:05-27

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 :)

CodePudding user response:

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))

CodePudding user response:

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

for word in mylist:
    ele = word.split("'")
    if len(ele) > 1:
        for i in range (0,len(ele)):
            if i == 0:
                newlist.append(ele[i] "'")
            else:
                newlist.append(ele[i])
    else:
        newlist.append(ele[0])

print (mylist)
print (newlist)

CodePudding user response:

A (slightly) simpler variation on previous answers, that makes no assumptions about the strings in your list:

newlist = []

for word in mylist:
    bits = word.split("'")
    for w in bits[:-1]:
        newlist.append(w "'")
    newlist.append(bits[-1])

CodePudding user response:

mylist = ['La', 'domestication', "d'un", 'animal', 'ou', "d'un", 'végétal,', 'necessite', "l'acquisition", "d'une", 'ferme']
new_list = []
for word in mylist:
    if "'" in word:
        parts = word.split("'")
        for i in range(len(parts)-1):
            new_lst.append(parts[i] "'")
        new_list.append(parts[-1])
    else:
        new_list.append(word)

CodePudding user response:

I just modified your code.

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

lol = str("")
print(lol)
for word in mylist:
    
    if "'" in word:
        new_words = word.split("'")
        for i in new_words:
            lol=lol "0" str(i).encode('ascii', 'ignore').decode('ascii')
    else:
        lol=lol "0" str(word).encode('ascii', 'ignore').decode('ascii')
        
if lol[0]=="0":
    lol = lol[1:]
    
lol = lol.split("0")
print(lol)

CodePudding user response:

You can use enumerate() to get the index you are iterating at, then use array slicing to replace at the correct position.

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

for i, w in enumerate(mylist):
    if "'" in w:
        mylist[i:i 1] = w.split("'") 

Now printing my mylist gives us:

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

Edit: This is bad code. Don't do this.

  • Related