Py2neo:查找关系并返回节点

2024-09-28 20:15:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如果不使用图形.cypher.execute(或获得图形.cypher.execute返回一组节点而不是它的难看的返回字符串)

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) RETURN tom,tomHanksMovies

我想要的是:

^{pr2}$

其中a[0]给出tom-hanks节点,a[1]给出关系,a[2]给出电影。在


编辑:添加了“错误”示例输出

>>> print('List all Tom Hanks movies...')
>>> a = graph.cypher.execute('MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) RETURN tomHanksMovies')
>>> print(a)

List all Tom Hanks movies...
    | tomHanksMovies                                                                                                                                                                 
----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1 | (n4354:Movie {released:1998,tagline:"At odds in life... in love on-line.",title:"You've Got Mail"})                                                                            
  2 | (n4360:Movie {released:1993,tagline:"What if someone you never met, someone you never saw, someone you never knew was the only someone for you?",title:"Sleepless in Seattle"})
  3 | (n4365:Movie {released:1990,tagline:"A story of love, lava and burning desire.",title:"Joe Versus the Volcano"})                                                               
  4 | (n4372:Movie {released:1996,tagline:"In every life there comes a time when that thing you dream becomes that thing you do",title:"That Thing You Do"})                         
  5 | (n4392:Movie {released:2012,tagline:"Everything is connected",title:"Cloud Atlas"})                                                                                            
  6 | (n4398:Movie {released:2006,tagline:"Break The Codes",title:"The Da Vinci Code"})                                                                                              
  7 | (n4417:Movie {released:1999,tagline:"Walk a mile you'll never forget.",title:"The Green Mile"})                                                                                
  8 | (n4431:Movie {released:1995,tagline:"Houston, we have a problem.",title:"Apollo 13"})                                                                                          
  9 | (n4437:Movie {released:2000,tagline:"At the edge of the world, his journey begins.",title:"Cast Away"})                                                                        
 10 | (n4446:Movie {released:2007,tagline:"A stiff drink. A little mascara. A lot of nerve. Who said they couldn't bring down the Soviet empire.",title:"Charlie Wilson's War"})     
 11 | (n4448:Movie {released:2004,tagline:"This Holiday Season… Believe",title:"The Polar Express"})                                                                                 
 12 | (n4449:Movie {released:1992,tagline:"Once in a lifetime you get a chance to do something different.",title:"A League of Their Own"})   


>>> print(a[0])

 tomHanksMovies                                                                                     
-----------------------------------------------------------------------------------------------------
 (n4354:Movie {released:1998,tagline:"At odds in life... in love on-line.",title:"You've Got Mail"})

>>> print(type(a[0]))

<class 'py2neo.cypher.core.Record'>

Tags: theinyoutitlemoviecypherprinttom
2条回答

如果你喜欢使用py2neo,你可以做的就是稍微修改你的查询,以返回你想要的所有信息。例如

a = graph.cypher.execute("MATCH (a:Person {name:"Tom Hanks"})-[acted:ACTED_IN]->(movies:Movie) RETURN a, acted, movies")

它应该做的是把结果列在一个列表中,就像你说的你不想要的那样。但是,从这里你可以索引结果,得到你想要的每个部分。例如,[0]将给出第一行结果,[0][0]将给出第一行结果中的person节点,[0][0][0]将给出第一行第一个节点的第一个属性,等等。从这里可以运行For循环,将结果组织成您更感兴趣的表单。在

希望这有帮助。在

我想您应该知道execute实际上并没有返回一个字符串?您在a中看到的是文档中指定的RecordList的表示:

http://py2neo.org/2.0/cypher.html#py2neo.cypher.CypherResource.execute

a[0]然后给你这个RecordList中的第一个Record。然后可以通过名称访问Record中的值,例如a[0]['tomHanksMovies']。在

Record对象的详细信息如下:

http://py2neo.org/2.0/cypher.html#records

相关问题 更多 >