首页 > 解决方案 > Python:决策树条件

问题描述

我正在尝试用 Python 编写我的部分决策树逻辑。数据来自数据框 1 中的列表 1 和数据框 2 中的列表 2 和列表 3。它指出:如果列表 2 和列表 3 中的项目都在列表 1 中的某处找到并且列表 2 和列表 3 中的项目并排不是同样,然后打印出 List 2 项目。我不知道如何解决这个问题。嵌套for循环?或者有没有最好用的功能。如果有人能指出我正确的方向。谢谢!

for items 2 in list 2 AND for items 3 in list 3: for items 1 in list 1: if items 2 == items 1 AND if items 3 == items 1 AND if items 2 = [x for x, y in zip(list 2, list 3) if x != y]: then print items 2 in a list

标签: pythonconditional-statements

解决方案


你的逻辑会沿着这条路走

list_1 = df1["List 1"].values  # List 1 from DataFrame1
list_2_and_3 = df2[["List 2", "List 3"]].values  # List 2 and 3 from DataFrame2

for element in list_2_and_3:
    # element[0] is from List 2
    # element[1] is from List 3
    if element[0] in list_1 and element[1] in list_1:  # if both are present in the list 1
        if element[0] != element[1]:  # If they are not the same
            print(element[0])  # print List 2 items


推荐阅读