简体   繁体   English

如何替换列表中的部分字符串?

[英]How can I replace parts of strings in a list?

I want to replace part of a string in a list in python 2.7, but this doesn't work the way I expect: 我想在python 2.7中替换列表中字符串的一部分,但这不能按我期望的方式工作:

>>> list = ['apples', 'oranges', 'bananas']
>>> for item in list: 
...  item=item.replace('app', 'spam') 
... 
>>> list
['apples', 'oranges', 'bananas']

I expected the output to be: ['spamles', 'oranges', 'bananas'] . 我希望输出为: ['spamles', 'oranges', 'bananas']

What's the right way of doing the above? 做上述事情的正确方法是什么? I know I can probably make a new list and add each item to the new list individually, but that sounds like a lot of work and might require twice the memory. 我知道我可能可以制作一个新列表并将每个项目分别添加到新列表中,但这听起来像是很多工作,并且可能需要两倍的内存。

Please note list is not a good local variable name. 请注意list不是一个好的本地变量名称。

The reason your code is not working is, you are not replacing the corresponding element in the loop, you are just assigning it to the local variable. 您的代码无法正常工作的原因是,您没有在循环中替换相应的元素,只是将其分配给了局部变量。

Try this: 尝试这个:

x = ['apples', 'oranges', 'bananas']
for i, item in enumerate(x): 
    item=item.replace('app', 'spam') 
    x[i] = item
    #Or just 
    #x[i] = item.replace('app', 'spam') 

Demo: 演示:

>>> x = ['apples', 'oranges', 'bananas']
>>> for i, item in enumerate(x): 
...     item=item.replace('app', 'spam') 
...     x[i] = item
... 
>>> x
['spamles', 'oranges', 'bananas']
>>> 

Alternatively, you can use a list comprehensions : 另外,您可以使用列表推导

>>> x =  ['apples', 'oranges', 'bananas']
>>> x = [a.replace("app", "spam") for a in x ]
>>> x
['spamles', 'oranges', 'bananas']
>>> 
for item in list: 

This creates a new reference ( item ) to each successive element in your list. 这将为列表中的每个后续元素创建一个新引用( item )。 You are then rebinding your reference item to something else, and then throwing that ref away. 然后,您将参考item重新绑定到其他item ,然后将其扔掉。

I know I can probably make a new list and add each item to the new list individually, but that sounds like a lot of work and might require twice the memory. 我知道我可能可以制作一个新列表并将每个项目分别添加到新列表中,但这听起来像是很多工作,并且可能需要两倍的内存。

Premature optimization is the root of.. some evil. 过早的优化是...邪恶的根源。 Probably not all evil, but some subset of it. 可能不是所有的邪恶,而是其中的一部分。

But seriously, the memory overhead of temporarily creating a list of 3 (or even 3 thousand) strings is not worth thinking about. 但是,严重的是,暂时创建3个(甚至3000个)字符串列表的内存开销不值得考虑。 Just make a new list: 只需列出一个新清单:

li = ['apples', 'oranges', 'bananas']
li = [x.replace('app','spam') for x in li]
#li = ['spamles', 'oranges', 'bananas']

(don't name your lists list , it shadows the built-in) (不要命名您的列表list ,它会隐藏内置list

If you absolutely, positively have to mutate your list in-place: 如果绝对的话,肯定需要就地更改列表:

for i,x in enumerate(li):
    li[i] = x.replace('app','spam')

And while this doesn't create a new list (temporarily saving memory), it is actually going to be slower than the list-comprehension version, so the "optimization" really didn't get you anywhere. 尽管这不会创建一个新列表(暂时节省内存),但实际上它会比列表理解版本要慢,因此“优化”实际上并没有帮助您。

You just reassign to the variable named item , when what you want is to assign to the list-item: 当您想要分配给列表item ,只需将其重新分配给名为item的变量:

for i, item in enumerate(list):
    list[i] = item.replace('app', 'spam') 

This code is for demonstration only. 此代码仅用于演示。 The real solution is using a list comprehension, like in @karthikr's answer. 真正的解决方案是使用列表推导,就像@karthikr的答案一样。

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

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