简体   繁体   English

替换列表中的元素 - 使用包含所述元素的嵌套列表(python3)

[英]Replace elements in list - with a nested list containing said elements (python3)

I am trying to replace elements in a list with a list containig said elements, for example:我正在尝试用包含所述元素的列表替换列表中的元素,例如:

list = ['cow','orange','apple','mango']
to_replace = 'orange', 'apple'
replace_with = ['banana','cream']

Desired output:所需的 output:

['cow', ['banana', 'cream'], 'mango']

I've read the answers in this question 1 and tried the function mentioned there:我已阅读此问题1中的答案并尝试了此处提到的 function:

def replace_item(list, to_replace, replace_with):
    for n,i in enumerate(list):
      if i== to_replace:
         list[n]=replace_with
    return list

but it does nothing.但它什么也没做。 I also tried a basic equality functions:我还尝试了一个基本的相等函数:

list[1:3] = ['banana','cream']

However neither work.但是,两者都不起作用。 Is there a function to accomplish this?有没有 function 来完成这个? In my actual problem which is a loop, list[1:3] is an iterator, ie list[i:i+2] .在我的实际问题中,它是一个循环, list[1:3]是一个迭代器,即list[i:i+2] I am not sure if this makes a difference.我不确定这是否会有所作为。 The items to be replaced will always be sequential.要替换的项目将始终是连续的。

Try this:尝试这个:

List[1:3] = [["banana", "cream"]]

The reason this isnt working is because when you are writing: List[1:3] = ["banana", "cream"] you are replacing a list with a list.这不起作用的原因是因为当您编写时: List[1:3] = ["banana", "cream"]您正在用列表替换列表。 In other words you are changing ["orange", "apple"] to ["banana", "cream"] .换句话说,您将["orange", "apple"]更改为["banana", "cream"]

When doing this: List[1:3] = [["banana", "cream"]] you replace a list with a list in a list, replacing ["orange", "apple"] with [["banana","cream"]] , thereby replacing "orange", "apple" with ["banana", "cream"]执行此操作时: List[1:3] = [["banana", "cream"]]将列表替换为列表中的列表,将["orange", "apple"]替换为[["banana","cream"]] ,从而将"orange", "apple"替换为["banana", "cream"]

def replace_item(lst, to_replace, replace_with):
    for n,i in enumerate(lst):
      if i in to_replace and lst[n+1] in to_replace:
         lst[n]=replace_with
         lst[n+1] = ""
    return [item for item in lst if item]

lst = ['cow','orange','apple','mango']
to_replace = 'orange', 'apple'
replace_with = ['banana','cream']

print (replace_item(lst, to_replace, replace_with))

Output: Output:

['cow', ['banana', 'cream'], 'mango']

First, don't call your var the same name as object type.首先,不要将您的 var 称为与 object 类型相同的名称。 That is a bad idea that can lead to code confusion/bugs etc.这是一个坏主意,可能导致代码混乱/错误等。

In your function, the problem was essentially that the == operator between a string ( i ) and a list ( lst ) will never say True regardless the rest.在您的 function 中,问题本质上是字符串( i )和列表( lst )之间的==运算符永远不会说True无论 rest。 Plus, the content in string in the list is not the same as in your string.另外,列表中字符串中的内容与您的字符串中的内容不同。 So you use in instead of == .所以你使用in而不是==

After this, if you don't want to have a duplicate and wish to replace adjacent 'orange', 'apple' by a single ['banana','cream'] , you need to check lst[n+1] when iterating.在此之后,如果您不想重复并希望将相邻'orange', 'apple'替换为单个['banana','cream'] ,则需要在迭代时检查lst[n+1] .

Then you replace lst[n] , set lst[n+1] to nothing and return a list purged from nothing elements using list comprehension.然后你替换lst[n] ,将lst[n+1]设置为空,并返回一个使用列表推导从空元素中清除的列表。

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

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