Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向Python列表添加数据_Python_Python 2.7 - Fatal编程技术网

向Python列表添加数据

向Python列表添加数据,python,python-2.7,Python,Python 2.7,我刚刚开始使用python列表,我编写了下面的简单代码,希望打印的文件显示数字[12,14,16,18,20,22],但只显示22。任何帮助都会很好 a=10 b=14 while a <= 20: a=a+2 b=b-1 datapoints=[] datapoints.insert(0,a) print datapoints a=10 b=14 而aa=10 b=14 datapoints=[]#这需要在循环之外建立 而aa=10 b=14 datap

我刚刚开始使用python列表,我编写了下面的简单代码,希望打印的文件显示数字[12,14,16,18,20,22],但只显示22。任何帮助都会很好

a=10
b=14
while a <= 20:
    a=a+2
    b=b-1
    datapoints=[]
    datapoints.insert(0,a)
print datapoints
a=10
b=14
而a
a=10
b=14
datapoints=[]#这需要在循环之外建立
而a
a=10
b=14
datapoints=[]#这需要在循环之外建立

虽然Joel已经回答了,但是如果你想要一个更紧凑的代码,你可以使用range

numbers = []
for number in range(12,24,2):
    # do whatever you want with b
    numbers.append(number)

print numbers
或者,如果您只想打印数字,您可以这样做

print [number for number in range(12,24,2)]

Joel已经回答了,但如果您想要更紧凑的代码,可以使用range

numbers = []
for number in range(12,24,2):
    # do whatever you want with b
    numbers.append(number)

print numbers
或者,如果您只想打印数字,您可以这样做

print [number for number in range(12,24,2)]

您可以使用该方法实现预期的列表作为输出。它需要三个参数,启动、停止和步进

data_points = range(12, 23, 2)  # range returns list in python 2
print data_points
请注意,在
python3
中。因此,在Python3中,您必须将其强制转换为
list

data_points = list(range(12, 23, 2))  # python 3
print(data_points) 

您可以使用该方法实现预期的列表作为输出。它需要三个参数,启动、停止和步进

data_points = range(12, 23, 2)  # range returns list in python 2
print data_points
请注意,在
python3
中。因此,在Python3中,您必须将其强制转换为
list

data_points = list(range(12, 23, 2))  # python 3
print(data_points) 

好的,您正在将
数据点设置为循环中的空列表。我建议您将
datapoints=[]
行移到循环之外。另外,如果您希望在末尾添加数字,那么请使用
list.append()
,这是您希望的输出。如果您希望在末尾添加数字,
b
是否有目的?或者如果您希望在前面添加数字,使用
collections.deque
可以调用
appendleft
并高效地执行预结束(
list
s在右侧只有有效的附录,预结束是
O(n)
)。
而对于这样的情况,
通常不是pythonic IMO,考虑使用<<代码> < /COD>循环,而不是结合<代码> Reals,您将“代码>数据池< /代码>设置为循环中的空列表。我建议您将
datapoints=[]
行移到循环之外。另外,如果您希望在末尾添加数字,那么请使用
list.append()
,这是您希望的输出。如果您希望在末尾添加数字,
b
是否有目的?或者如果您希望在前面添加数字,使用
collections.deque
可以调用
appendleft
并高效地执行预结束(
list
s在右侧只有有效的附录,预结束是
O(n)
)。
而对于这样的情况,
通常不是pythonic IMO,考虑使用<代码> < /COR>循环,而不是结合<代码>范围< /代码>欢呼,我对这个很陌生,有时很难看到树的树梢。欢呼,我对它很陌生,有时很难看到树的树林。