如何拆分一个元素并将列表中的两个部分替换为 python

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

我有一个这样的列表:

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

我想将内部具有“'”的元素拆分为 2 个元素,并将它们的索引保留在原始列表中。

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

我尝试了一些东西,但我承认我坚持要替换正确索引中的两个新拆分元素,这是我尝试过的代码:

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)

感谢您的宝贵时间和帮助:)

假设您对创建新列表感到满意,实现这一目标的一种方法是将现有列表与 space 一起加入,然后拆分为 space 或 ':

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

输出:

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

请注意,这是假设列表中的单词没有 space;如果可能的话,您可以将代码中的 space 替换为单词中未出现的字符(或字符序列),例如[=14=]:

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

您可以使用enumerate()获取您正在迭代的索引,然后使用数组切片在正确的位置进行替换。

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("'") 

现在打印我的 mylist 给我们:

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

编辑:这是错误的代码。不要这样做。

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)

我刚刚修改了你的代码。

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

先前答案的(稍微)更简单的变体,不对列表中的字符串做任何假设:

newlist = []

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