Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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 我正在尝试将这个for循环转换为列表理解?_Python_List_Loops_For Loop_List Comprehension - Fatal编程技术网

Python 我正在尝试将这个for循环转换为列表理解?

Python 我正在尝试将这个for循环转换为列表理解?,python,list,loops,for-loop,list-comprehension,Python,List,Loops,For Loop,List Comprehension,这将给出所需的结果-对于所需范围内的i,生成元组(1,0,0…),然后展平元组列表。最后,附加尾随的1 L=[] for i in range(11): L.append(1) for z in range(i): L.append(0) L.append(1) #this is to add a 1 at the end of list print(L) 你可以让它成为一种纯粹的理解:[x代表i在范围内(-1,11)代表x在(0,)*i+(1,)] >

这将给出所需的结果-对于所需范围内的
i
,生成元组
(1,0,0…
),然后展平元组列表。最后,附加尾随的
1

L=[]
for i in range(11):
     L.append(1)
     for z in range(i):
        L.append(0)
L.append(1) #this is to add a 1 at the end of list
print(L)

你可以让它成为一种纯粹的理解:
[x代表i在范围内(-1,11)代表x在(0,)*i+(1,)]
>>> [x for i in range(11) for x in (1,)+(0,)*i] + [1]
[1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]