我正在尝试创建一个函数,将多个变量与一个整数进行比较,并输出一个由三个字母组成的字符串.我想知道是否有办法将其翻译成Python.所以说:
x = 0
y = 1
z = 3
mylist = []
if x or y or z == 0 :
mylist.append("c")
if x or y or z == 1 :
mylist.append("d")
if x or y or z == 2 :
mylist.append("e")
if x or y or z == 3 :
mylist.append("f")
Run Code Online (Sandbox Code Playgroud)
这将返回一个列表
["c", "d", "f"]
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?
如果我有这样的结构:
def foo():
a=None
b=None
c=None
#...loop over a config file or command line options...
if a is not None and b is not None and c is not None:
doSomething(a,b,c)
else:
print "A config parameter is missing..."
Run Code Online (Sandbox Code Playgroud)
python中首选的语法是检查所有变量是否都设置为有用的值?是我写的还是其他更好的方式?
这与这个问题不同: 不是Python中的无测试 ......我正在寻找检查许多条件是否为None的首选方法.我输入的选项似乎很长而且非pythonic.