【问题标题】:Gremlin Python: unhashable type: 'dict' while using groupCount on edgesGremlin Python:不可散列的类型:'dict'同时在边缘上使用groupCount
【发布时间】:2020-02-28 11:28:31
【问题描述】:

我正在尝试计算边缘介数。该查询在 gremlin 控制台上运行良好,但在 gremlin-python 中不起作用。

g.V().as_("v").
  repeat(identity().as_("src").
         bothE().as_("e").
         bothV().as_("v").
         where(neq("src")).
         simplePath()).
    emit().
  filter(project("x","y","z").
           by(select(first, "v")).
           by(select(last, "v")).
           by(select("v").count(local)).as_("triple").
         coalesce(select("x","y").as_("a").
                  select("triples").unfold().as_("t").
                  select("x","y").
                  where(eq("a")).
                  select("t"),
                  store("triples")).
         select("z").as_("length").
         select("triple").
         select("z").
         where(eq("length"))).
   select('e').
   unfold().
   groupCount()

错误是:TypeError: unhashable type: 'dict'

如果我将其更改为顶点中间性,那么它就可以正常工作。我觉得的问题是如何在 python 中检索边缘,它是一张地图。当我进行分组计数时,它还会创建一个映射,其中键作为边缘,值作为计数。在 python 中,键本身不能是映射,因此会引发此错误。

如何解决这个问题?另外请说明如何在 gremlin-python 中使用select(all, 'e')

【问题讨论】:

    标签: python gremlin gremlinpython


    【解决方案1】:

    您遇到了 gremlinpython 的 limitations 之一,因为 Gremlin 可以返回 Python 中不存在的 dict 值。您需要将这些键转换为可以在 Python 中作为键存在的东西,同时保留键包含的信息。我没有您的数据或输出样本,但我设计了以下内容作为演示:

    gremlin> g.V().both().elementMap().groupCount().unfold()
    ==>{id=5, label=software, name=ripple, lang=java}=1
    ==>{id=2, label=person, name=vadas, age=27}=1
    ==>{id=4, label=person, name=josh, age=32}=3
    ==>{id=3, label=software, name=lop, lang=java}=3
    ==>{id=1, label=person, name=marko, age=29}=3
    ==>{id=6, label=person, name=peter, age=35}=1
    

    使用dict 作为键,这在 python 中不起作用,我们会得到与你现在得到的相同的错误。有许多选项可用于将此结果改造成 python 可以使用的东西,但这里有一个简单的选项,只是为了让您思考您可能会做什么:

    gremlin> g.V().both().elementMap().groupCount().unfold().map(union(select(keys),select(values)).fold())
    ==>[[id:5,label:software,name:ripple,lang:java],1]
    ==>[[id:2,label:person,name:vadas,age:27],1]
    ==>[[id:4,label:person,name:josh,age:32],3]
    ==>[[id:3,label:software,name:lop,lang:java],3]
    ==>[[id:1,label:person,name:marko,age:29],3]
    ==>[[id:6,label:person,name:peter,age:35],1]
    

    在上面,我将dict 解构为一对list。现在您在客户端知道每个结果都是服务器端dict 中的一个条目,其中对中的第一个值是键,第二个是值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多