Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 手动限制Graql查询结果迭代器_Python_Python 3.x_Querying_Vaticle Typedb_Vaticle Typeql - Fatal编程技术网

Python 手动限制Graql查询结果迭代器

Python 手动限制Graql查询结果迭代器,python,python-3.x,querying,vaticle-typedb,vaticle-typeql,Python,Python 3.x,Querying,Vaticle Typedb,Vaticle Typeql,如果我接受一个简单的查询,比如match$x isa dog;限值5;得到然后无论Grakn中存储了多少只狗,我都会得到5个结果。这很好,但是如果我在进行查询时不知道需要多少只狗,并且想限制稍后在代码中检索的狗的数量,该怎么办 下面是我使用Python客户端的想法: import grakn client = grakn.Grakn(uri="localhost:48555") session = client.session(keyspace="dogs_keyspace") tx = ses

如果我接受一个简单的查询,比如
match$x isa dog;限值5;得到然后无论Grakn中存储了多少只狗,我都会得到5个结果。这很好,但是如果我在进行查询时不知道需要多少只狗,并且想限制稍后在代码中检索的狗的数量,该怎么办

下面是我使用Python客户端的想法:

import grakn
client = grakn.Grakn(uri="localhost:48555")
session = client.session(keyspace="dogs_keyspace")
tx = session.transaction(grakn.TxType.WRITE)

results = tx.query('match $x isa dog; get;')  # I don't limit now, so I can do it later
results
是一个迭代器,所以我不能这样做:

limited_results = list(results)[:5]
因为如果我这样做了,那么所有的狗都会被放到列表中,然后我会选择前5只,如果我在知识图表中有1000000只狗的话,这是非常低效的

但我可以说:

limited_results = list(itertools.islice(results, 5))
我应该只养前5条狗,不碰其他99995条狗


但我的问题是:有没有任何原因表明这种方法比在查询中提供
limit 5
要慢,比如
match$x isa dog;限值5;得到

如果您不希望Grakn检索图中的所有
狗,并且只访问前5个,那么这两种方法都是有效的,因为它们都使用惰性迭代器,这意味着在您明确请求下一个结果之前不会尝试检索

如果您直接发出查询
match$x isa dog;限值5;得到,Grakn将构建一个迭代器,它将只对其进行5次迭代,并将结果返回给客户端