在 Python 中将整数附加到列表的开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17911091/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:27:29  来源:igfitidea点击:

Append integer to beginning of list in Python

pythonlistvariablesappend

提问by gen

I have an integer and a list. I would like to make a new list of them beginning with the variable and ending with the list. Writing a + listI get errors. The compiler handles aas integer, thus I cannot use append, or extend either. How would you do this?

我有一个整数和一个列表。我想制作一个以变量开头并以列表结尾的新列表。写a + list我得到错误。编译器处理a为整数,因此我不能使用附加或扩展。你会怎么做?

采纳答案by Nullify

>>>var=7
>>>array = [1,2,3,4,5,6]
>>>array.insert(0,var)
>>>array
[7, 1, 2, 3, 4, 5, 6]

How it works:

这个怎么运作:

array.insert(index, value)

array.insert(index, value)

Insert an item at a given position. The first argument is the index of the element before which to insert, so array.insert(0, x)inserts at the front of the list, and array.insert(len(array), x)is equivalent to array.append(x).Negative values are treated as being relative to the end of the array.

在给定位置插入一个项目。第一个参数是要插入的元素的索引,因此array.insert(0, x)在列表的前面插入,array.insert(len(array), x)相当于array.append(x).Negative 值被视为相对于数组的末尾。

回答by Rohit Jain

>>> a = 5
>>> li = [1, 2, 3]
>>> [a] + li  # Don't use 'list' as variable name.
[5, 1, 2, 3]

回答by v2b

Another way of doing the same,

做同样的另一种方式,

list[0:0] = [a]

回答by timgeb

Note that if you are trying to do that operation often, especially in loops, a list is the wrong data structure.

请注意,如果您经常尝试执行该操作,尤其是在循环中,则列表是错误的数据结构

Lists are not optimized for modifications at the front, and somelist.insert(0, something)is an O(n) operation.

列表没有针对前面的修改进行优化,并且somelist.insert(0, something)是一个O(n) 操作

somelist.pop(0)and del somelist[0]are also O(n) operations.

somelist.pop(0)并且del somelist[0]也是 O(n) 操作。

The correct data structure to use is a dequefrom the collectionsmodule. deques expose an interface that is similar to those of lists, but are optimized for modifications from both endpoints. They have an appendleftmethod for insertions at the front.

要使用的正确数据结构deque来自collections模块。双端队列公开了一个类似于列表的接口,但针对来自两个端点的修改进行了优化。他们有一种appendleft在前面插入的方法。

Demo:

演示:

In [1]: lst = [0]*1000
In [2]: timeit -n1000 lst.insert(0, 1)
1000 loops, best of 3: 794 ns per loop
In [3]: from collections import deque
In [4]: deq = deque([0]*1000)
In [5]: timeit -n1000 deq.appendleft(1)
1000 loops, best of 3: 73 ns per loop

回答by Erico9001

New lists can be made by simply adding lists together.

可以通过简单地将列表添加在一起来制作新列表。

list1 = ['value1','value2','value3']
list2 = ['value0']
newlist=list2+list1
print(newlist)

回答by aparna ramesh

None of these worked for me. I converted the first element to be part of a series (a single element series), and converted the second element also to be a series, and used append function.

这些都不适合我。我将第一个元素转换为系列(单个元素系列)的一部分,并将第二个元素也转换为系列,并使用 append 函数。

l = ((pd.Series(<first element>)).append(pd.Series(<list of other elements>))).tolist()

回答by Dovi Salomon

Based on some (minimal) benchmarks using the timeitmodule it seems that the following has similar if not better performance than the accepted answer

基于使用该timeit模块的一些(最小)基准测试,以下内容的性能似乎与接受的答案相似,如果不是更好的话

new_lst = [a, *lst]

As with [a] + listthis will create a new list and not mutate lst.

[a] + list这将创建一个新列表而不是 mutate lst

If your intention is to mutate the list then use lst.insert(0, a).

如果您的目的是改变列表,请使用lst.insert(0, a).

回答by sahil panindre

list_1.insert(0,ur_data)

make sure that ur_data is of string type so if u have data= int(5)convert it to ur_data = str(data)

确保 ur_data 是字符串类型,所以如果你已经data= int(5)将它转换为ur_data = str(data)

回答by HoangYell

You can use Unpack list:

您可以使用解包列表:

a = 5

li = [1,2,3]

li = [a, *li]

=> [5, 1, 2, 3]

一 = 5

li = [1,2,3]

li = [a, *li]

=> [5, 1, 2, 3]

回答by Benyamin Jafari

Alternative:

选择:

>>> from collections import deque

>>> my_list = deque()
>>> my_list.append(1)       # append right
>>> my_list.append(2)       # append right
>>> my_list.append(3)       # append right
>>> my_list.appendleft(100) # append left
>>> my_list

deque([100, 1, 2, 3])

>>> my_list[0]

100


[NOTE]:

[注意]:

collections.dequeis faster than Python pure listin a loop Relevant-Post.

collections.dequelist在循环Relevant-Post 中比纯 Python 快。