# python - title用法 - plt.text用法

## 如何根据值测试多个变量? (10)

``````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")
``````

``````["c", "d", "f"]
``````

Set是这里的好方法，因为它定义了变量，这里似乎是你的目标。 无论参数的顺序如何`{z,y,x}`都是`{0,1,3}`

``````>>> ["cdef"[i] for i in {z,x,y}]
['c', 'd', 'f']
``````

``````if 1 in {x, y, z}:
``````

``````if 0 in {x, y, z}:
Mylist.append("c")
elif 1 in {x, y, z}:
Mylist.append("d")
...
``````

``````["c"]
``````

``````if 0 in {x, y, z}:
Mylist.append("c")
if 1 in {x, y, z}:
Mylist.append("d")
if 2 in {x, y, z}:
Mylist.append("e")
...
``````

``````numToLetters = {0:"c", 1:"d", 2:"e", 3:"f"}
for number in numToLetters:
if number in {x, y, z}:
Mylist.append(numToLetters[number])
``````

`````` x = 0
y = 1
z = 3

Mylist = []

if x == 0 or y==0 or z==0:
Mylist.append('c')
elif x == 1 or y == 1 or z==1:
Mylist.append('d')
elif x==2 or y==2 or z==2:
Mylist.append('e')
elif x==3 or y==3 or z==3:
Mylist.append('f')
``````

``````if x == 1 or y == 1 or z == 1:
``````

``````if 1 in (x, y, z):
``````

``````if 1 in {x, y, z}:
``````

`x or y or z`将计算为'truthy'的第一个参数，例如not `False` ，数字0或空值（有关Python在布尔上下文中认为错误的详细信息，请参阅布尔表达式 ）。

`x or y or z == 0`的直接方式是

``````if any(map((lambda value: value == 0), (x,y,z))):
``````

``````0 in (x, y, z)
``````

``````my_cases = {
0: Mylist.append("c"),
1: Mylist.append("d")
# ..
}

for key in my_cases:
if key in (x,y,z):
my_cases[key]()
break
``````

``````myList = []
aList = [0,1,3]

for l in aList:
if l==0:myList.append('c')
elif l==1:myList.append('d')
elif l==2:myList.append('e')
elif l==3:myList.append('f')

print(myList)
``````

``````my_dict = {0: "c", 1: "d", 2: "e", 3: "f"}

def validate(x, y, z):
for ele in [x, y, z]:
if ele in my_dict.keys():
return my_dict[ele]
``````

``````print validate(0, 8, 9)
c
print validate(9, 8, 9)
None
print validate(9, 8, 2)
e
``````

``````L ={x, y, z}
T= ((0,"c"),(1,"d"),(2,"e"),(3,"f"),)
List2=[]
for t in T :
if t[0] in L :
List2.append(t[1])
break;
``````

``````>>> x="Wild things"
>>> y="throttle it back"
>>> z="in the beginning"
>>> if "Wild" in {x,y,z}: print (True)
...
``````

``````>>> x="Wild things"
>>> y="throttle it back"
>>> z="in the beginning"
>>> if "Wild" in [x,y,z]: print (True)
...
``````

``````>>> [x,y,z]
['Wild things', 'throttle it back', 'in the beginning']
>>> {x,y,z}
{'in the beginning', 'throttle it back', 'Wild things'}
>>>

>>> if "Wild" in str([x,y,z]): print (True)
...
True
>>> if "Wild" in str({x,y,z}): print (True)
...
True
``````

``````d = {0:'c', 1:'d', 2:'e', 3: 'f'}
x, y, z = (0, 1, 3)
print [v for (k,v) in d.items() if x==k or y==k or z==k]
``````