Python:GQuery结果集(GQL,GAE)上的DISTINCT [英] Python: DISTINCT on GQuery result set (GQL, GAE)
问题描述
您希望执行以下不支持的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屋!