python - 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
解决方案
你的逻辑会沿着这条路走
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
推荐阅读
- java - 如何防止 Spotless/Eclipse 加入行?
- influxdb - InfluxDB:查询聚合但处理丢失的数据
- python - Python有效地检查和创建多个csv文件路径
- c# - 如何检查字典是否包含字符串中的字母
- loops - 带有 sympy 的基本循环给了我这个错误信息:'Symbol' object is not subscriptable
- reactjs - 设置状态时重新渲染过多 - useSWR
- ubuntu - Onedrive 同步未检测到远程更改 - Ubuntu 18.04
- php - 如何正确连接 mysqli 和 PHP
- excel - Power Query 仅加载前 20 列
- arrays - 如何从 Dart 的列表中查找缺失的数字