我试图制作一个函数,将多个变量与一个整数进行比较,并输出一个三个字母的字符串。我想知道是否有办法将其翻译成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")

其将返回以下列表:

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

当前回答

首先,对或有条件的:

你需要说:

if x == 0 or y == 0 or z == 0:

原因是“或”将条件拆分为单独的逻辑部分。按照你最初陈述的方式,这些部分是:

x
y
z == 0   // or 1, 2, 3 depending on the if statement

最后一部分很好——例如,检查z==0,但前两部分基本上说了如果x和如果y。因为整数总是求值为True,除非它们为0,这意味着当x或y不等于0时,条件的第一部分始终为True(在y的情况下,因为y=1,所以整个条件(因为or的工作方式)始终为True)。

为了避免这种情况,您需要确保条件的所有部分(OR的每一侧)都有意义(您可以假装OR语句的另一侧不存在)。这就是您确认or条件是否正确定义的方法。

您可以这样单独编写声明:

if x == 0
if y == 0
if z == 0

这意味着与OR关键字的正确合并将是:

if x == 0 or y == 0 or z == 0

第二,如何解决问题:

您基本上希望检查是否有任何变量与给定的整数匹配,如果匹配,请在一对一映射中为其分配一个匹配的字母。您希望对某个整数列表执行此操作,以便输出为字母列表。你可以这样做:

def func(x, y, z):

    result = []

    for integer, letter in zip([0, 1, 2, 3], ['c', 'd', 'e', 'f']):
        if x == integer or y == integer or z == integer:
            result.append(letter)
            
    return result
        

同样,您可以使用LIST COMPRESENSION更快地获得相同的结果:

def func(x, y, z):

    return [ 
                letter 
                for integer, letter in zip([0, 1, 2, 3], ['c', 'd', 'e', 'f'])
                if x == integer or y == integer or z == integer
           ]
    
    

其他回答

这里提供的所有优秀答案都集中于原始海报的具体要求,并集中于Martijn Pieters提出的if1in{x,y,z}解决方案。他们忽略了问题的更广泛含义:如何针对多个值测试一个变量?如果使用字符串,则提供的解决方案不适用于部分命中,例如:测试字符串“Wild”是否为多个值

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

or

>>> 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

然而,需要注意的是,正如@codeforester所提到的,这个方法会丢失单词boundries,如:

>>> x=['Wild things', 'throttle it back', 'in the beginning']
>>> if "rot" in str(x): print(True)
... 
True

这三个字母rot在列表中以组合形式存在,但不是单独的单词。测试“腐烂”会失败,但如果列表中的一项是“腐烂在地狱”,那也会失败。结果是,如果使用此方法,请注意搜索条件,并注意它确实有此限制。

您的问题更容易通过字典结构解决,如:

x = 0
y = 1
z = 3
d = {0: 'c', 1:'d', 2:'e', 3:'f'}
mylist = [d[k] for k in [x, y, z]]

要使用一个值测试多个变量:如果{a,b,c}中为1:

要使用一个变量测试多个值:如果{1,2,3}中有一个:

写x或y或z==0的直接方法是

if any(map((lambda value: value == 0), (x,y,z))):
    pass # write your logic.

但我认为,你不喜欢这种方式很难看。

另一种方式(更好)是:

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

也许您需要输出位集的直接公式。

x=0 or y=0 or z=0   is equivalent to x*y*z = 0

x=1 or y=1 or z=1   is equivalent to (x-1)*(y-1)*(z-1)=0

x=2 or y=2 or z=2   is equivalent to (x-2)*(y-2)*(z-2)=0

让我们映射到位:“c”:1“d”:0xb10“e”:0xb 100“f”:0xb1 000

isc的关系(为“c”):

if xyz=0 then isc=1 else isc=0

使用数学if公式https://youtu.be/KAdKCgBGK0k?list=PLnI9xbPdZUAmUL8htSl6vToPQRRN3hhFp&t=315

[c] :(xyz=0和isc=1)或((xyz=0和isc=1)或(isc=0))和(isc=0))

[d] :((x-1)(y-1)(z-1)=0且isc=2)或((xyz=0且isd=2)或(isc=0))

...

通过以下逻辑连接这些公式:

逻辑和是方程的平方和逻辑或是方程式的乘积

你会得到一个总方程式求和,你就有了求和的总公式

那么和1是c,和2是d,和4是e,和5是f

在此之后,您可以形成预定义的数组,其中字符串元素的索引将对应于就绪字符串。

array[sum]提供字符串。