[英]How to take multiple input in python for list and tuple by comprehension
如何在一行中为元组和列表获取多个输入?
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.