Python:理解附加和扩展之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29513126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: understanding difference between append and extend
提问by AlanH
The code below will not run in its current state. However, if I change sum_vec.extend( vec1[i] + vec2[i] )
to sum_vec.append( vec1[i] + vec2[i] )
it works just fine. I understand the basic different between append and extend, but I don't understand why the code doesn't work if I use extend.
下面的代码不会在当前状态下运行。但是,如果我更改sum_vec.extend( vec1[i] + vec2[i] )
为sum_vec.append( vec1[i] + vec2[i] )
它工作得很好。我理解 append 和 extend 之间的基本区别,但我不明白为什么如果我使用扩展,代码就不起作用。
def addVectors(v1, v2):
vec1 = list(v1)
vec2 = list(v2)
sum_vec = []
vec1_len = len(vec1)
vec2_len = len(vec2)
min_len = min( vec1_len, vec2_len )
# adding up elements pointwise
if vec1_len == 0 and vec2_len == 0:
return sum_vec
else:
for i in xrange(0, min_len):
sum_vec.extend( vec1[i] + vec2[i] )
# in case one vector is longer than the other
if vec1_len != vec2_len:
if vec1_len > vec2_len:
sum_vec.extend( vec1[min_len : vec1_len] )
else:
sum_vec.extend( vec2[min_len : vec2_len] )
print sum_vec
return sum_vec
v1 = [1,3,5]
v2 = [2,4,6,8,10]
addVectors(v1,v2)
采纳答案by chepner
As others have pointed out, extend
takes an iterable (such as a list, tuple or string), and adds each element of the iterable to the list one at a time, while append
adds its argument to the end of the list as a single item. The key thing to note is that extend
is a more efficient version of calling append
multiple times.
正如其他人指出的那样,extend
接受一个可迭代对象(例如列表、元组或字符串),并将可迭代对象的每个元素一次一个append
添加到列表中,同时将其参数作为单个项目添加到列表的末尾。需要注意的关键是多次extend
调用的更有效版本append
。
a = [1,2]
b = [1,2]
a.extend([3, 4])
for x in [3, 4]:
b.append(x)
assert a == b
append
can take an iterable as its argument, but it treats it as a single object:
append
可以将迭代作为其参数,但它将其视为单个对象:
a = [1,2]
a.append([3,4])
assert a == [1, 2, [3, 4]] # not [1, 2, 3, 4]
回答by Reut Sharabani
You can read the documentation on list:
您可以阅读list上的文档:
list.append
adds a single item to the end of your list:
list.append
在列表末尾添加一个项目:
Add an item to the end of the list; equivalent to a[len(a):] = [x].
在列表末尾添加一个项目;等价于 a[len(a):] = [x]。
list.extend
uses an iterable and adds all of it's elements to the end of your list:
list.extend
使用可迭代并将其所有元素添加到列表的末尾:
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
通过附加给定列表中的所有项目来扩展列表;相当于 a[len(a):] = L。
You need to use:
您需要使用:
sum_vec.extend([vec1[i] + vec2[i]]) # note that a list is created
That way an iterable with a single item (vec1[i] + vec2[i]
) is passed. But list.append
is more suitable when you're always adding a single item.
这样vec1[i] + vec2[i]
就传递了一个带有单个项目 ( )的可迭代对象。但是list.append
当您总是添加单个项目时更适合。
回答by aldeb
When you run your code, you get an exception like this:
当你运行你的代码时,你会得到一个这样的异常:
Traceback (most recent call last):
File ".../stack.py", line 28, in <module>
addVectors(v1,v2)
File ".../stack.py", line 15, in addVectors
sum_vec.extend( vec1[i] + vec2[i] )
TypeError: 'int' object is not iterable
In other words, the extend
method expects an iterable as argument.
However append method gets an item as argument.
换句话说,该extend
方法需要一个可迭代对象作为参数。但是 append 方法获取一个项目作为参数。
Here is a small example of the difference between extend and append:
这是扩展和追加之间区别的一个小例子:
l = [1, 2, 3, 4]
m = [10, 11]
r = list(m)
m.append(l)
r.extend(l)
print(m)
print(r)
Output:
输出:
[10, 11, [1, 2, 3, 4]]
[10, 11, 1, 2, 3, 4]
回答by Marek
extend needs an iterable as a parameter if you want to extend list by a single element you need to dress is in a list
如果您想通过单个元素扩展列表,则扩展需要一个可迭代作为参数,您需要装扮在列表中
a = []
b = 1
a.extend([b])
a.append(b)
回答by Haresh Shyara
The method append adds its parameter as a single element to the list, while extend gets a list and adds its content.
方法 append 将其参数作为单个元素添加到列表中,而 extend 获取列表并添加其内容。
letters = ['a', 'b']
letters.extend(['c', 'd'])
print(letters) # ['a', 'b', 'c', 'd']
letters.append(['e', 'f'])
print(letters) # ['a', 'b', 'c', 'd', ['e', 'f']]
names = ['Foo', 'Bar']
names.append('Baz')
print(names) # ['Foo', 'Bar', 'Baz']
names.extend('Moo')
print(names) # ['Foo', 'Bar', 'Baz', 'M', 'o', 'o']