Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 如何在while循环后创建列表_Python_List_While Loop - Fatal编程技术网

Python 如何在while循环后创建列表

Python 如何在while循环后创建列表,python,list,while-loop,Python,List,While Loop,我有一个关于如何在while循环之后创建列表的问题。我想把我从while循环中得到的所有数字都放到一个列表中 例如: x=4 while(1): print(x) x=x+1 if x==8:break 然后我得到 4 5 6 7 我想在一个列表中显示这些数字 l=[] x=4 while(1): print(x) l.append(x) x=x+1 if x==8:break print(l) 这就是您将其添加到代码中的方式。

我有一个关于如何在while循环之后创建列表的问题。我想把我从while循环中得到的所有数字都放到一个列表中 例如:

x=4
while(1):
    print(x)
    x=x+1
    if x==8:break
然后我得到

4
5
6
7
我想在一个列表中显示这些数字

l=[]
x=4

while(1):
    print(x)
    l.append(x)

    x=x+1
    if x==8:break

print(l)
这就是您将其添加到代码中的方式。仅供参考,如果你想用“Pythonic”的方式来做,它很简单:

l = range(4, 8)
这就是您将其添加到代码中的方式。仅供参考,如果你想用“Pythonic”的方式来做,它很简单:

l = range(4, 8)
L=[]
i=4
而i
L=[]
i=4

而我您正在寻找append()函数。有关更多信息,请查看

list=[] #declare a blank list to use later
x=4

while(1):
    list.append(x) #add x to the list
    x += 1 # a shorthand way to add 1 to x
    if x == 8:break

print(list) #after the loop is finished, print the list

您正在寻找append()函数。有关更多信息,请查看

list=[] #declare a blank list to use later
x=4

while(1):
    list.append(x) #add x to the list
    x += 1 # a shorthand way to add 1 to x
    if x == 8:break

print(list) #after the loop is finished, print the list