简体   繁体   English

Python:追加/扩展方法

[英]Python: append/ extend method

Can someone explain me why after the for loop the list res is ['m'] ?有人可以解释一下为什么在 for 循环之后列表 res 是['m']吗?

string = 'spam'
for x in string:
    res =[]
    res.extend(x)
print(res)

I expected the output to be res = ['s', 'p', 'a', 'm']我预计 output 是res = ['s', 'p', 'a', 'm']

You are replacing the list object each step of your loop .您将在循环的每个步骤中替换列表 object 。 The statement res = [] creates a new, empty list object, then adds a single letter to that list.语句res = []创建一个新的空列表 object,然后在该列表中添加一个字母。

Without the loop, this is what you are doing:没有循环,这就是你正在做的事情:

>>> x = 's'
>>> res = []
>>> res.extend(x)
>>> res
['s']
>>> x = 'p'
>>> res = []
>>> res.extend(x)
['p']
>>> x = 'a'
>>> res = []
>>> res.extend(x)
>>> res
['a']
>>> res = []
>>> x = 'm'
>>> res.extend(x)
>>> res
['m']

Create the list outside of the loop, once :在循环之外创建列表,一次

string = 'spam'
res = []
for x in string:
    res.extend(x)
print(res)

Now you don't keep replacing the list object with a new one each iteration of the for loop.现在,您不必在for循环的每次迭代中都用一个新列表替换列表 object。

Again, removing the loop and doing the steps manually, now we have:同样,删除循环并手动执行这些步骤,现在我们有:

>>> res = []
>>> x = 's'
>>> res.extend(x)
>>> res
['s']
>>> x = 'p'
>>> res.extend(x)
>>> res
['s', 'p']
>>> x = 'a'
>>> res.extend(x)
>>> res
['s', 'p', 'a']
>>> x = 'm'
>>> res.extend(x)
>>> res
['s', 'p', 'a', 'm']

Not that you should be using res.extend() here;并不是说您应该在这里使用res.extend() it only works because individual letters in string assigned to x are each also strings and even single-letter strings are still sequences.它之所以有效,是因为分配给xstring中的各个字母也是字符串,甚至单字母字符串仍然是序列。 What you are really doing with res.extend(x) is the equivalent of for element in x: res.append(element) , but x will always have just one element.你真正用res.extend(x)做的事情相当于for element in x: res.append(element) ,但x总是只有一个元素。

So this would work too:所以这也可以:

string = 'spam'
res = []
for x in string:
    res.append(x)
print(res)

or just extend res with the whole string value:或者只是用整个string值扩展res

string = 'spam'
res = []
res.extend(string)
print(res)

or, if you just wanted a list of all the characters of a string, just use the list() function:或者,如果您只是想要一个字符串的所有字符的列表,只需使用list() function:

string = 'spam'
res = list(string)
print(res)

list() does exactly what you wanted to do with your loop: create an empty list, loop over the input, and add each element to the new list, which is then returned: list()完全按照您想要对循环执行的操作:创建一个空列表,循环输入,并将每个元素添加到新列表中,然后返回:

>>> string = 'spam'
>>> list(string)
['s', 'p', 'a', 'm']

You'll never get that output because for every iteration of the loop, you are setting res = [] and therefore only the last iteration will work by extending the blank list with 'm' .您永远不会得到 output 因为对于循环的每次迭代,您都在设置res = [] ,因此只有最后一次迭代可以通过使用'm'扩展空白列表来工作。

The fixed code looks like this:固定代码如下所示:

string = 'spam'
res = []
for x in string:
    res.extend(x)
print(res)

Another note is that you probably should use .append in this case.另一个注意事项是,在这种情况下,您可能应该使用.append .extend is for appending an entire iterable but since you are only adding one element at a time it isn't necessary. .extend用于附加整个可迭代对象,但由于您一次只添加一个元素,因此没有必要。 Check here for a good explanation. 在这里查看一个很好的解释。

Also a last note here is that you'll want to be careful with editing python code outside of plain text or code editors.最后要注意的是,在纯文本或代码编辑器之外编辑 python 代码时要小心。 You're using some leading and trailing apostrophes '' instead of regular '' which will cause you trouble at some point.您正在使用一些前导和尾随撇号''而不是常规''这会在某些时候给您带来麻烦。

You are always re-initializing the res list in the for-loop, that is why in the last iteration of the loop the list is initialized to [] an empty list and the last letter is added to it.您总是在 for 循环中重新初始化res列表,这就是为什么在循环的最后一次迭代中,列表被初始化为[]一个空列表并将最后一个字母添加到其中。

string = 'spam'
res =[]
for x in string:
    res.extend(x)
print(res)

or to make it simple, use the list builtin which takes an iterable like a string and converts it into an list object:或者为了简单起见,使用内置list ,它接受一个像字符串一样的可迭代并将其转换为列表object:

>>> list('spam')
['s', 'p', 'a', 'm']

You are resetting res every time inside the loop.您每次在循环内都重置 res 。 You need to use this-你需要使用这个-

string = ‘spam’
res =[]
for x in string:   
    res.extend(x)
print(res)

I think this is simplest way:我认为这是最简单的方法:

string = 'spam'
res = list(string)

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

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