首页 > 解决方案 > 我需要帮助在我的代码中形成 if else 条件

问题描述

我需要在我的代码中形成 if else 条件。我正在将两个列表相互比较并打印每个列表中是否有任何不同的项目或没有。我的代码如下。

title_check = [[y for y in xml_title_list if y not in json_title_list], [y for y in json_title_list if y not in xml_title_list]]
if len(title_check [0]):
    print("Type which are unmatched in XML are ", title_check [0])

if len(title_check [1]):
    print("Type which are unmatched in JSON are ", type_check[1])

else:
    print("No Unmatched type found") 

我想结合两个 if 条件并希望每次都运行它。如果他们不执行我想要的那么。现在它只将else条件作为第二个if条件。有人能帮我吗?提前致谢

标签: pythonpython-3.xloops

解决方案


您可以使用另一个答案中所示的标志。另一种方法是

if len(title_check[0]) or len(title_check[1]):
    if len(title_check[0]):
        print("Type which are unmatched in XML are ", type_check[0])

    if len(title_check[1]):
        print("Type which are unmatched in JSON are ", type_check[1])
else:
    print("No Unmatched type found") 

推荐阅读