简体   繁体   English

如何将密码查询值存储到python函数内的变量中?

[英]How can I store the cypher query value into a variable inside python function?

I want to store the price average of the data that I have inside my neo4j database, so I can then use the python variable to define if the product is cheap or expensive. 我想存储我的neo4j数据库中数据的平均价格,因此我可以使用python变量来定义产品是便宜还是昂贵。

I already have the cypher query which is giving me all the average prices of all services that I have in my database. 我已经有了密码查询,可以查询数据库中所有服务的平均价格。 Now I want to store this query result into a variable or list. 现在,我想将此查询结果存储到变量或列表中。 So I can use it to define if the price is cheap or expensive (if price <= average) it is cheap otherwise it is expensive. 因此,我可以使用它来定义价格是便宜还是昂贵(如果价格<=平均)是便宜的,否则是昂贵的。


      def average(self):

           query = '''
              MATCH (p:Price)-[:COSTS]-(s:Service)
              RETURN avg(toFloat(p.monthly)) AS Average, p.currency AS Currency, s.service_name AS Service
               '''
           return  graph.run(query, average= self.Average, currency= self.Currency, service_name=self.Service )

Can anyone help me out with this problem? 谁能帮我解决这个问题?

Your query can return multiple rows since it specifies Currency and Service as the unique grouping keys for the AVG aggregating function . 您的查询可以返回多行,因为它将CurrencyService指定为AVG 聚合功能的唯一分组键。 So average() should probably return something like a list of dictionaries. 因此, average()应该返回类似字典列表的内容。

The run() function returns a Cursor , which has a data() function that will "extract the entire result as a list of dictionaries". run()函数返回一个Cursor ,它具有一个data()函数,该函数将“将整个结果提取为字典列表”。

So, this version of average() should return a list of dictionaries for the query results: 因此,此版本的average()应该返回查询结果的字典列表:

  def average(self):
       query = '''
          MATCH (p:Price)-[:COSTS]-(s:Service)
          RETURN avg(toFloat(p.monthly)) AS Average, p.currency AS Currency, s.service_name AS Service
           '''
       return graph.run(query).data()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中的函数内给变量另一个值? - how can I give a variable another value inside a function in python? 我不知道如何在数据库上执行SUM函数并将值存储到python变量中 - I can't figure out how to perform the SUM function on a DB and store the value into a python variable 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 如何将某些内容附加到函数的结果中并将其存储在python中的变量中 - How can I append something to a result of a function and store that in a Variable in python 如何在另一个 python 脚本中访问函数内的变量 - How can I access a variable inside a function in another python script 如何将当前变量值传递给python函数? - How can I always pass the current variable value into a python function? 如何在phpmyadmin的数据库中存储python变量的值? - how to store the python variable's value inside the database in phpmyadmin? Python-如果在函数内部创建变量,那么如何在函数外部使用它? - Python - If I create a variable inside a function, how can I then use it outside the function? 我怎样才能得到一个函数来存储一个新变量,并在它被传递后分配一个值 - how can I get a function to store a new variable, with a value assigned to it, after it has been passed 无法在“with”块内更改函数变量的值 python - Can't change a value of a function's variable inside a "with" block python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM