python - 我需要帮助在我的代码中形成 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条件。有人能帮我吗?提前致谢
解决方案
您可以使用另一个答案中所示的标志。另一种方法是
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")
推荐阅读
- javascript - appendChild 在新创建的 div 中
- javascript - 从反应组件中的类访问方法
- intellij-idea - SBT + Idea 2.5 := 无法解析符号
- python - 函数集成返回“只有长度为 1 的数组可以转换为 Python 标量”
- tensorflow - 使用 softmax_cross_entropy_with_logits 和“软”标签/目标在 Tensorflow 中的成本函数收敛
- python - 使用 seaborn,如何将不同颜色的数据点添加到我的散点图中或更改为最后一个数据点的颜色?
- php - 如何通过一个查询从多个数据库中下载所有变量?
- multithreading - 无法在 python 3 中重新填充队列
- java - 内容不适合 ScrollPane。无法滚动直到结束
- c++ - 需要帮助制作构造函数