Py学习  »  Python

python使用if语句验证多个值

sagar_dev • 5 年前 • 1562 次点击  

示例1检查单个值:

def validate(self, data, check, col):
    output = []
    for key, value in data.iteritems():
        try:
            if value[col] == check:
                output.append("{},{},Passed".format(key,value[col]))
            else:
                output.append("{},{},Failed".format(key,value[col]))
        except IndexError as e:
            pass
    return output


data1 = {key1:['a','b','c','ok'], key2:['a','b','c','down']}
check = "ok"
col = 3
print self.validate(data1, check, col)

输出:

['key1','ok','Passed', 'key2','down','Failed']

示例2检查两个值:

 data1 = {key1:['a','b','c','ok', "True"], key2:['a','b','c','down', "False"]}

 def validate(self, data, check1, check2, col1, col2):
    output = []
    for key, value in data.iteritems():
        try:
            if value[col1] == check1 and value[col2] == check2:
                output.append("{},{},{},Passed".format(key,value[col1], value[col2]))
            else:
                output.append("{},{},Failed".format(key,value[col1], value[col2]))
        except IndexError as e:
            pass
    return output

有没有一种方法可以在一个代码中实现上述两个示例(验证多个值1、2、3、4)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43135
 
1562 次点击  
文章 [ 2 ]  |  最新文章 5 年前