[英]Elegant way to add elements in a list if the elements are not None
I have the following codes which add a
and b
into my_input
when they are not None.我有以下代码,当它们不是无时将
a
和b
添加到my_input
中。 The code works fine, but I am wondering is there a more elegant way to do this?代码工作正常,但我想知道有没有更优雅的方法来做到这一点? Thanks!
谢谢!
if a is not None and b is not None
my_input = [
[a, b]
] + my_lib.get_my_list()
else:
my_input = my_lib.get_my_list()
Maybe something like this:也许是这样的:
[[a, b] if a is not None and b is not None else my_lib.my_list()]
I just find a new way to do this:我只是找到了一种新的方法来做到这一点:
my_input = []
(a is not None and b is not None) and my_input.append([a, b])
my_input = my_input + my_lib.get_my_list()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.