本文翻译自:Append integer to beginning of list in Python

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 + list I get errors. a + list会出错。 The compiler handles a as integer, thus I cannot use append, or extend either. 编译器将a作为整数处理,因此我不能使用append或extend。 How would you do this? 你会怎么做?


#1楼

参考:https://stackoom.com/question/1D9UZ/在Python中将整数附加到列表的开头


#2楼

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

#3楼

>>>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)

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) 。负值被视为相对于数组的末尾。


#4楼

另一种方式,

list[0:0] = [a]

#5楼

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 deque from the collections module. 正确使用的数据结构是来自collections模块的deque deques expose an interface that is similar to those of lists, but are optimized for modifications from both endpoints. deques公开了与列表类似的接口,但针对两个端点的修改进行了优化。 They have an appendleft method for insertions at the front. 他们在前面有一个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

#6楼

New lists can be made by simply adding lists together. 只需将列表加在一起即可创建新列表。

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

在Python中将整数附加到列表的开头相关推荐

  1. 如何在Python中将字典键作为列表返回?

    本文翻译自:How to return dictionary keys as a list in Python? In Python 2.7 , I could get dictionary keys ...

  2. python使用字典格式化字符串-Python中将(字典,列表等)变量格式化输出

    Python中将(字典,列表等)变量格式化成(漂亮的,树形的,带缩进的,JSON方式的)字符串输出: 变量类型是列表,列表中每个值是个字典类型变量. 格式化输出的效果,希望是那种树状结构,带缩进的,而 ...

  3. 如何在Python中将元素添加到列表

    In this tutorial, we will learn different ways to add elements to a List in Python. 在本教程中,我们将学习使用Pyt ...

  4. python中将整数转化为八进制的函数,Python进制转化

    Python中的进制有二进制.八进制.十进制.十六进制,用python的内置函数可以方便的进行不同进制之间的转换,二.八.十六进制数字表示前面分别添加0b.0o.0x(前面为零). 二进制 八进制 十 ...

  5. python中如何追加_如何在Python中将元素添加到列表中-追加,扩展和插入

    在Python中使用列表时,您通常会希望向列表中添加新元素. Python列表数据类型具有三种添加元素的方法:append()-将单个元素追加到列表. extend() -将iterable的元素添加 ...

  6. python列表可以混合类型_如何在Python中将混合数据类型的列表转换为数据帧

    我有一个混合数据类型列表,如下所示:list = [['3D prototypes', 'Can print large objects', 'Autodesk Maya/Mudbox', '3D S ...

  7. python 数字转十六进制_在Python中将整数转换为十六进制

    In Python I want to tranform the integer 3892 into a hexcode with the given format and the result \x ...

  8. python列表转化为数字信号_在python中将声音转换为音素列表

    准确的音素识别不容易存档,因为音素本身的定义相当松散.即使在好的音频中,现在最好的系统也有18%的音素错误率(你可以在Alex Graves发布的TIMIT上查看LSTM-RNN结果). 在cmusp ...

  9. 如何在Python中将一个字符串附加到另一个字符串?

    我想要一种有效的方法在Python中将一个字符串附加到另一个字符串. var1 = "foo" var2 = "bar" var3 = var1 + var2 ...

最新文章

  1. Java IO: 管道
  2. 使用OpenCV可视化Intel Realsensen D435 深度图为彩色图
  3. 信息学奥赛C++语言: 直角三角形
  4. Magento中直接使用SQL语句
  5. JRebel 启动报错 could not be processed by xxx
  6. 视觉SLAM——ORB特征
  7. XPath学习:轴(4)——ancestor
  8. Windows移动开发(一)——登堂入室
  9. 电力系统卫星时钟同步工作的重要性
  10. MATLAB:绘制用户给定的01序列图并计算其对应的AMI编码,双相编码和CMI编码
  11. 快递企业设长租公寓解决住宿 降低快递员流动率
  12. VMI(供应商管理库存)模式及其改进方式探讨
  13. 51单片机调整时钟Proteus仿真
  14. Ubuntu wine QQ 微信乱码
  15. 罐子与硬币--【英雄会】
  16. XShell免费正版远程控制ssh客户端
  17. 亚马逊云计算服务将支持甲骨文数据库
  18. DDK开发介绍_自我学习
  19. 应用ceph文件系统存储(ceph-13.2.10)
  20. 1命名规则 sentinel_哨兵-1A数据命名规则

热门文章

  1. 接口转发和重定向区别(三)
  2. 深入理解WMS(一):Window的创建过程
  3. (0033) iOS 开发之Block 的基础用法及注意事项2
  4. 一个页面区分管理者和普通用户如何设计_产品经理要做的操作权限/数据权限设计...
  5. linux 文件截取
  6. node.js学习随笔
  7. 【iOS】Xcode 使用 CocoaPods 导入第三方库后没有提示
  8. android-创建流式布局,并修改最后一行的最后一个view
  9. 互联网创业如何与传统行业人士合作?
  10. 近看图灵碗 (8. 我就是上帝) (上)