如何在python中通过推导式接受列表和元组的多个输入



如何在一行中接受元组和列表的多个输入?

a= list(x = int(input()) for x in range(n)  if x > 0)
''' it has an error , how can I solve it and use similar code for tuple '''

您可以在您的理解中使用filter,如下所示:

a = list(filter(lambda x: x>0, (int(input()) for _ in range(n))))

对于n = 5和输入:

1
0
3
-10
4

它会打印:

[1, 3, 4]

最新更新