如何在 Python 中的一行中附加多个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16621498/
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
How to append multiple items in one line in Python
提问by whatever
I have:
我有:
count = 0
i = 0
while count < len(mylist):
if mylist[i + 1] == mylist[i + 13] and mylist[i + 2] == mylist[i + 14]:
print mylist[i + 1], mylist[i + 2]
newlist.append(mylist[i + 1])
newlist.append(mylist[i + 2])
newlist.append(mylist[i + 7])
newlist.append(mylist[i + 8])
newlist.append(mylist[i + 9])
newlist.append(mylist[i + 10])
newlist.append(mylist[i + 13])
newlist.append(mylist[i + 14])
newlist.append(mylist[i + 19])
newlist.append(mylist[i + 20])
newlist.append(mylist[i + 21])
newlist.append(mylist[i + 22])
count = count + 1
i = i + 12
I wanted to make the newlist.append()statements into a few statements.
我想把这些newlist.append()陈述分成几个陈述。
采纳答案by Ignacio Vazquez-Abrams
No. The method for appending an entire sequence is list.extend().
否。附加整个序列的方法是list.extend()。
>>> L = [1, 2]
>>> L.extend((3, 4, 5))
>>> L
[1, 2, 3, 4, 5]
回答by Karl Knechtel
No.
不。
First off, appendis a function, so you can't write append[i+1:i+4]because you're trying to get a slice of a thing that isn't a sequence. (You can't get an element of it, either: append[i+1]is wrong for the same reason.) When you call a function, the argument goes in parentheses, i.e. the round ones: ().
首先,append是一个函数,所以你不能写,append[i+1:i+4]因为你试图得到一个不是序列的东西。(你也不能得到它的一个元素:append[i+1]出于同样的原因是错误的。)当你调用一个函数时,参数放在括号中,即圆括号:()。
Second, what you're trying to do is "take a sequence, and put every element in it at the end of this other sequence, in the original order". That's spelled extend. appendis "take this thing, and put it at the end of the list, as a single item, even if it's also a list". (Recall that a list is a kind of sequence.)
其次,您要做的是“取一个序列,并将其中的每个元素按原始顺序放在另一个序列的末尾”。那是拼写extend。append是“把这个东西,放在列表的末尾,作为一个单独的项目,即使它也是一个列表”。(回想一下,列表是一种序列。)
But then, you need to be aware that i+1:i+4is a special construct that appears only inside square brackets (to get a slice from a sequence) and braces (to create a dictobject). You cannot pass it to a function. So you can't extendwith that. You need to make a sequence of those values, and the natural way to do this is with the rangefunction.
但是,您需要注意这i+1:i+4是一个特殊的构造,它只出现在方括号(从序列中获取切片)和大括号(创建dict对象)内。您不能将其传递给函数。所以你不能extend这样做。您需要创建这些值的序列,而自然的方法是使用range函数。
回答by Kevin Postlewaite
You could also:
你也可以:
newlist += mylist[i:i+22]
回答by saoud rehman
mylist = [1,2,3]
def multiple_appends(listname, *element):
listname.extend(element)
multiple_appends(mylist, 4, 5, "string", False)
print(mylist)
OUTPUT:
输出:
[1, 2, 3, 4, 5, 'string', False]
回答by Idrisi_Kasim
Use this :
用这个 :
#Inputs
L1 = [1, 2]
L2 = [3,4,5]
#Code
L1+L2
#Output
[1, 2, 3, 4, 5]
By using the (+) operator you can skip the multiple append & extend operators in just one line of code and this is valid for more then two of lists by L1+L2+L3+L4.......etc.
通过使用 (+) 运算符,您可以在一行代码中跳过多个附加和扩展运算符,这对 L1+L2+L3+L4....... 等的两个以上列表有效。
Happy Learning...:)
快乐学习...:)

