一个具有未知条件数的语句python [英] one if statement with unknown number of conditions python

查看:156
本文介绍了一个具有未知条件数的语句python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表,其中包含if语句必须满足的所有条件,但问题是存储在列表列表中的条件数是未知的。例如,列表列表如下:

I have a list of list that contains all the conditions that the if statement has to satisfy, but the problem is that the number of conditions stored into the list of list is unknown. For e.g., the list of list is like this:

my_list: [["A", "0"], ["B", "1"], ["C", "2"]]

所以if应该是:

if A==0 and B==1 and C==2:
      #do-something
else:
      pass

因为我不知道列表列表中的元素,我做不到

since I don't know the number of elements in the list of lists, I cannot do:

if my_list[0][0]==my_list[0][1] and my_list[1][0]==my_list[1][1] and my_list[2][0]==my_list[2][1]:
     #do-something
else:
      pass

如何解决这个问题?

出现了类似的问题这里但是没有明确解释/实现这个问题。

A similar problem has been raised here but there is no a clear explanation/implementation of this problem.

谢谢。

推荐答案

您可以使用 生成器表达式 all()

You can use a generator expression within all():

if all(i == j for i, j in my_list): # use int(j) if 'j' is string and 'i' is integer.
    # do something

这篇关于一个具有未知条件数的语句python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆