有什么办法可以在 python 中使用 List comprehension 编写此代码?
Is there any way to write this code with List comprehension in python?
为了计算 result 的值,正确编写此 List Comprehension 的方法应该是什么?
nothing = [0,0,0,0,0,0]
box = [1,2]
boxes = [box,box,box]
page = [boxes,boxes]
pages = [page, page]
npr = [nothing, pages]
**result = [box for box in npr.pages[i].boxes] where i is the counter variable**
P.S。这是伪代码。
像这样:
result = [i for boxes in npr[1] for i in boxes]
print(result)
输出:
[1, 2, 3, 4, 5, 6]
在这种情况下,为什么不直接使用:
result = npr[1][0]
或者:
result = sum(npr[1], [])
为了计算 result 的值,正确编写此 List Comprehension 的方法应该是什么?
nothing = [0,0,0,0,0,0]
box = [1,2]
boxes = [box,box,box]
page = [boxes,boxes]
pages = [page, page]
npr = [nothing, pages]
**result = [box for box in npr.pages[i].boxes] where i is the counter variable**
P.S。这是伪代码。
像这样:
result = [i for boxes in npr[1] for i in boxes]
print(result)
输出:
[1, 2, 3, 4, 5, 6]
在这种情况下,为什么不直接使用:
result = npr[1][0]
或者:
result = sum(npr[1], [])