博客
关于我
python 查询Neo4j多节点的多层关系
阅读量:798 次
发布时间:2023-03-07

本文共 1725 字,大约阅读时间需要 5 分钟。

使用Neo4j进行三元关系数据查询,基于关联规则提取满足3人及3案有关系的集合。以下是优化后的技术说明:

graph = Graph(host="192.168.3.186://7474", auth=("neo4j", "wabjtam123"))data = graph.run('match data=(p:anjian)-[:参与 *2..5]-(u:anjian) with p,collect(distinct u.AJBH) as ajbhs match (p:anjian)-[:参与 *1..5]-(c:xyr) with p,ajbhs, collect(distinct c.XYRBH) as xyrbh_list,(ajbhs+p.AJBH) as ajbh_list where size(xyrbh_list)>2 and size(ajbh_list)>2 return p.AJBH as ajbh,ajbh_list,xyrbh_list')aj = []xyr_result = []aj_result = []for node in data:    ajbh = node.get('ajbh')    ajbh_list = node.get('ajbh_list')    xyrbh_list = node.get('xyrbh_list')        # 去重处理    ajbh_list = list(set(ajbh_list))    xyrbh_list = list(set(xyrbh_list))        flag = True    if len(aj_result) > 0:        for val in ajbh_list:            if find(val, aj_result):                flag = False                break    if flag:        if len(ajbh_list) > 2 and len(xyrbh_list) > 2:            aj.append(ajbh)            aj_result.append(ajbh_list)            xyr_result.append(xyrbh_list)            print(f'匹配结果:{len(xyrbh_list)}人{len(ajbh_list)}案')            print(f'当前总数:{len(aj_result)}')            print(f'当前项目:{aj}')            print('----------------------------')            print(aj_result)            print('----------------------------')            print(aj_result)
conn = psycopg2.connect(database="zfgfh", user="postgres", password="postgres", host="192.168.3.202", port="5432")cursor = conn.cursor()for ajbh in aj_result:    for temp in ajbh:        insert_sql = "insert into test.gm (\"ajbh\") values ('%s')" % temp        print(insert_sql)        cursor.execute(insert_sql)conn.commit()cursor.close()

以上代码实现了三元关系数据的关联规则挖掘,通过Cypher查询从数据库中提取满足3人3案关联关系的三元组。数据处理流程包括去重、存储以及最终插入数据库的操作。

转载地址:http://qtofk.baihongyu.com/

你可能感兴趣的文章
Python 内嵌函数:它们有什么用处?
查看>>
Python 内置 sum 函数 vs. for 循环性能
查看>>
python 内置slice的用法
查看>>
Python 内置时间模块
查看>>
python 内部如何实现命名元组?
查看>>
Python 写Android App性能:入门到高级
查看>>
python 写入json数据到数据库
查看>>
Python 出现 TypeError: ‘encoding‘ is an invalid keyword argument for this function 解决方法
查看>>
python 出现 TypeError: ‘list‘ object is not callable 的解决方法
查看>>
python 函数1
查看>>
python 函数学习
查看>>
Python 函数随笔 map()函数,lambda自定义函数
查看>>
Python 分析天气,告诉你中秋应该去哪里
查看>>
python 列表中dict中key排序
查看>>
python 列表函数
查看>>
Python 列表删除相同的元素
查看>>
Python 列表解析 大文件
查看>>
python 列表转字典方法
查看>>
Python 列表(List)概述-ChatGPT4o作答
查看>>
Python 初学者需要知道的四条建议
查看>>