Python 在数字行的前面添加一个数字

Python 在数字行的前面添加一个数字,python,rows,Python,Rows,在python中,有一个.append函数将数字添加到行的后面: coordinate_row.append(coordinate) 但是,我想在一行的前面添加一个数字。像这样: [1,2,3] 加4 我怎样才能做到这一点?您可以使用+操作符来实现这一点 >>> l = [1,2,3] >>> l = [4] + l >>> l [4, 1, 2, 3] 还是你的情况 coordinate_row = list(coordinate)

在python中,有一个.append函数将数字添加到行的后面:

coordinate_row.append(coordinate)
但是,我想在一行的前面添加一个数字。像这样:

[1,2,3]
加4


我怎样才能做到这一点?

您可以使用
+
操作符来实现这一点

>>> l = [1,2,3]
>>> l = [4] + l
>>> l
[4, 1, 2, 3]
还是你的情况

coordinate_row = list(coordinate) + coordinate_row
作为一项功能

def front_append(l, item):
    return [item] + l

>>> l = [1,2,3]
>>> l = front_append(l, 4)
>>> l
[4, 1, 2, 3]
或者,您可以使用位置
0

>>> l = [1,2,3]
>>> l.insert(0, 4)
>>> l
[4, 1, 2, 3]

您只需使用
+
运算符即可完成此操作

>>> l = [1,2,3]
>>> l = [4] + l
>>> l
[4, 1, 2, 3]
还是你的情况

coordinate_row = list(coordinate) + coordinate_row
作为一项功能

def front_append(l, item):
    return [item] + l

>>> l = [1,2,3]
>>> l = front_append(l, 4)
>>> l
[4, 1, 2, 3]
或者,您可以使用位置
0

>>> l = [1,2,3]
>>> l.insert(0, 4)
>>> l
[4, 1, 2, 3]

您只需使用
+
运算符即可完成此操作

>>> l = [1,2,3]
>>> l = [4] + l
>>> l
[4, 1, 2, 3]
还是你的情况

coordinate_row = list(coordinate) + coordinate_row
作为一项功能

def front_append(l, item):
    return [item] + l

>>> l = [1,2,3]
>>> l = front_append(l, 4)
>>> l
[4, 1, 2, 3]
或者,您可以使用位置
0

>>> l = [1,2,3]
>>> l.insert(0, 4)
>>> l
[4, 1, 2, 3]

您只需使用
+
运算符即可完成此操作

>>> l = [1,2,3]
>>> l = [4] + l
>>> l
[4, 1, 2, 3]
还是你的情况

coordinate_row = list(coordinate) + coordinate_row
作为一项功能

def front_append(l, item):
    return [item] + l

>>> l = [1,2,3]
>>> l = front_append(l, 4)
>>> l
[4, 1, 2, 3]
或者,您可以使用位置
0

>>> l = [1,2,3]
>>> l.insert(0, 4)
>>> l
[4, 1, 2, 3]

这将实现以下目的:

coordinate_row = [1, 2, 3]
coordinate = [4]
coordinate_row = coordinate + coordinate_row
print coordinate_row  # output:  [4, 1, 2, 3]

这将实现以下目的:

coordinate_row = [1, 2, 3]
coordinate = [4]
coordinate_row = coordinate + coordinate_row
print coordinate_row  # output:  [4, 1, 2, 3]

这将实现以下目的:

coordinate_row = [1, 2, 3]
coordinate = [4]
coordinate_row = coordinate + coordinate_row
print coordinate_row  # output:  [4, 1, 2, 3]

这将实现以下目的:

coordinate_row = [1, 2, 3]
coordinate = [4]
coordinate_row = coordinate + coordinate_row
print coordinate_row  # output:  [4, 1, 2, 3]

我想做一个函数来做这个,它是如何工作的?@Pieter写一个函数很简单。我想做一个函数来做这个,它是如何工作的?@Pieter写一个函数很简单。我想做一个函数来做这个,这将如何工作?@Pieter编写函数非常简单。我想创建一个函数来完成这项工作,这将如何工作?@Pieter编写函数非常简单。