【问题标题】:Checking probability of the if statement in Python more efficiently更有效地检查 Python 中 if 语句的概率
【发布时间】:2022-11-10 22:06:36
【问题描述】:

我有多个变量要传入 if、elif 和 else 语句。假设 3 个变量 a、b 和 c。这些只是包含数字的列表。但是我需要为变量的每个概率定义 if、elif 和 else 语句。

例如:

  • 如果其中一个变量 >0 对该变量执行某些操作,但传递其他变量。

基于概率,我知道所有的可能性,因此我根据这些可能性准备代码

weeks =9

a=[1,0,1,1,1,0,0,0,1]
b=[1,0,0,1,0,1,1,0,1]
c=[1,0,0,0,1,0,1,1,1]
for i in range (weeks):
    if i <= 0:
        (print('this is hypo'))
        
    else:    
        if(a[i] <= 0 and b[i] <= 0 and c[i] <= 0):  # no prod         0
            print(a[i],b[i],c[i],'no ne is working')
        elif(a[i] > 0 and b[i] <= 0 and c[i] <= 0): # only first      1
            print(a[i],b[i],c[i],'only a working')
        elif(a[i] > 0 and b[i] > 0 and c[i] <= 0): #first and second  1-2
            print(a[i],b[i],c[i],'a and b working')
        elif(a[i] > 0 and b[i] <= 0 and c[i] > 0): # first and third  1-3
            print(a[i],b[i],c[i], 'a and c working')
        elif(a[i] <= 0 and b[i] > 0 and c[i] <= 0): # only second     2
            print(a[i],b[i],c[i],'only b working')
        elif(a[i]<= 0 and b[i] > 0 and c[i] > 0): #second and third   2-3
            print(a[i],b[i],c[i],'b and c working')
        elif(a[i] <= 0 and b[i] <= 0 and c[i] > 0):     # only third  3
            print(a[i],b[i],c[i],'only c working')
        else:                            # all of are working         1-2-3
            print (a[i],b[i],c[i], 'all wokring')
        print('iteration number :',i)

我想要实现的是找到一种有效的方法来在几句话中传递这些可能性。处理 3 个变量并不是什么大问题,但是如果我想传递 10 个变量会发生什么。我需要分别定义每个概率吗?

【问题讨论】:

    标签: python python-3.x if-statement


    【解决方案1】:

    你可以统计所有正在工作的东西,如果没有或所有东西都在工作,那么从某种意义上说是短路,如果只有一些,那么打印那些:

    a=[1,0,1,1,1,0,0,0,1]
    b=[1,0,0,1,0,1,1,0,1]
    c=[1,0,0,0,1,0,1,1,1]
    
    for abc in zip(a, b, c):
        working = [x for x, y in zip("abc", abc) if y > 0]
        print(
            *abc,
            "None" if not working else
            "All" if len(working) == len(abc) else
            "Only " + " and ".join(working),
            "working"
        )
    

    结果:

    1 1 1 All working
    0 0 0 None working
    1 0 0 Only a working
    1 1 0 Only a and b working
    1 0 1 Only a and c working
    0 1 0 Only b working
    0 1 1 Only b and c working
    0 0 1 Only c working
    1 1 1 All working
    

    【讨论】:

    • 不错的逻辑+1来自我的侧刺
    • 我试图理解@Jab 使用但没有正确理解的 elif 语句。能否得到详细的解释?
    • @Beginner我刚刚更新了答案,如果您在谈论join方法,它只会创建一个字符串,其中调用字符串join是分隔符。
    • @Jab 在这里更新后是我无法理解的部分 result = [x * z for x, y in zip("abc", abc) if (z := y > 0)] z = y 和大于 0,然后将其与 x 相乘?这个对吗 ?你能稍微扩展一下解释吗?我很感激
    • 当您将字符串乘以一个数字时,它会将其内容复制该数量,因此"a" * 0 == """a" * 1 == "a""a" * 5 == "aaaaa"(顺便说一句,布尔值是 int 1 或 0 的子集)。在理解中,条件 (z := y &gt; 0) 在结果 x * y 之前运行,因此我将 z 分配为布尔结果,然后如果它为 True,则执行乘法运算。在写这篇文章时,我发现我根本不需要分配 z 或进行乘法运算,因为 x 已经是所需的结果,我将编辑我的答案以反映。
    【解决方案2】:

    更简单的方法?

    a=[1,0,1,1,1,0,0,0,1]
    b=[1,0,0,1,0,1,1,0,1]
    c=[1,0,0,0,1,0,1,1,1]
    
    a1 = ['working' if int(el)>0 else 'not working' for el in a]
    b1 = ['working' if int(el)>0 else 'not working' for el in b]
    c1= ['working' if int(el)>0 else 'not working' for el in c]
    
    for f, b,i,x,y,z in zip(a, b,c,a1,b1,c1):
        print(f, b,i,x,y,z)
    

    输出 #

    1 1 1 working working working
    0 0 0 not working not working not working
    1 0 0 working not working not working
    1 1 0 working working not working
    1 0 1 working not working working
    0 1 0 not working working not working
    0 1 1 not working working working
    0 0 1 not working not working working
    1 1 1 working working working
    

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 2012-05-14
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多