【发布时间】:2021-05-24 13:49:46
【问题描述】:
我正在尝试在一行中编写一个包含 if-else 语句的列表。我已按照此中的说明进行操作 solution,但我遇到了多个 "indent-Expected" 错误。
这是我的代码:
initial= [[1,2,3],[],[]]
for block in initial:
'''
If block is empty move to the next block
'''
if not block: # empty?
continue # change the block
elif block: # full?
packet = block[0]
# True if there is an empty sublist
x = [True for i in initial if len(i) == 0]
# Enter if there is no empty sublist
if not any(x):
# Here is the issue
if packet > max([continue if sublist == block else sublist[-1] for sublist in initial]):
continue
block.remove(packet)
break # New-one
这一行的问题:
if packet > max([continue if sublist == block else sublist[-1] for sublist in initial]):
【问题讨论】:
-
continue是python中的保留关键字,你想干什么? -
@KrishnaChaurasia 在综合列表中,当我遍历初始列表的元素时,我需要忽略一些元素,因为我添加了
continue if sublist == block。 -
使用
0或负值,因为无论如何您都在使用列表中的max()。
标签: python for-loop conditional-statements list-comprehension