简体   繁体   English

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

[英]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.我想将内部具有“ ' ”的元素拆分为 2 个元素,并将它们的索引保留在原始列表中。

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:)感谢您的时间和帮助:)

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: 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, eg \0 :如果可能的话,您可以用单词中没有出现的字符(或字符序列)替换代码中的空格,例如\0

my_new_list = re.split(r"\0|(?<=')", '\0'.join(mylist))
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)

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

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)

You can use enumerate() to get the index you are iterating at, then use array slicing to replace at the correct position.您可以使用enumerate()获取要迭代的索引,然后使用数组切片替换正确的 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:现在打印我的mylist给我们:

['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.不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM