追加到Python列表开头整数 [英] Append integer to beginning of list in Python

查看:207
本文介绍了追加到Python列表开头整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数和一个列表。我想使它们与变量开始,以列表结束了一个新的列表。
A +列表我得到的错误。编译器手柄 A 为整数,所以我不能使用附加或扩展任。
你会怎样做呢?


解决方案

 >>>一个= 5
>>>利= [1,2,3]
>>> [A] +李#不要使用名单作为变量名。
[5,1,2,3]

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. The compiler handles a as integer, thus I cannot use append, or extend either. How would you do this?

解决方案

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

这篇关于追加到Python列表开头整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆