Python:GQuery结果集(GQL,GAE)上的DISTINCT [英] Python: DISTINCT on GQuery result set (GQL, GAE)

查看:137
本文介绍了Python:GQuery结果集(GQL,GAE)上的DISTINCT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在Google App Engine数据存储区中拥有实体,为匿名用户存储链接。
您希望执行以下不支持的SQL查询:

  SELECT DISTINCT user_hash FROM links 

您可以使用:

  user = db.GqlQuery(SELECT user_hash FROM links)

使用Python 最有效来过滤结果,因此它返回一个DISTINCT结果集?
如何计算DISTINCT结果集?

解决方案

一套是处理这个问题的好方法:

 >>> a = ['google.com','livejournal.com','livejournal.com','google.com','stackoverflow.com'] 
>>> b = set(a)
>>> b
set(['livejournal.com','google.com','stackoverflow.com'])
>>>

第一个答案是一个建议,即集合和字典在检索唯一结果很快,对于其他类型,列表中的成员资格为O(n)与O(1),所以如果您想存储其他数据,或者执行类似于创建 unique_results 列表中,最好做一些类似的操作:

  unique_results = {} 
>>>对于a中的项目:
unique_results [item] =''


>>> unique_results
{'livejournal.com':'','google.com':'','stackoverflow.com':''}


Imagine you got an entity in the Google App Engine datastore, storing links for anonymous users. You would like to perform the following SQL query, which is not supported:

SELECT DISTINCT user_hash FROM links

Instead you could use:

user = db.GqlQuery("SELECT user_hash FROM links")

How to use Python most efficiently to filter the result, so it returns a DISTINCT result set? How to count the DISTINCT result set?

解决方案

A set is good way to deal with that:

>>> a = ['google.com', 'livejournal.com', 'livejournal.com', 'google.com', 'stackoverflow.com']
>>> b = set(a)
>>> b
set(['livejournal.com', 'google.com', 'stackoverflow.com'])
>>> 

One suggestion w/r/t the first answer, is that sets and dicts are better at retrieving unique results quickly, membership in lists is O(n) versus O(1) for the other types, so if you want to store additional data, or do something like create the mentioned unique_results list, it may be better to do something like:

unique_results = {}
>>> for item in a:
    unique_results[item] = ''


>>> unique_results
{'livejournal.com': '', 'google.com': '', 'stackoverflow.com': ''}

这篇关于Python:GQuery结果集(GQL,GAE)上的DISTINCT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆