Python: Distinct On Gquery Result Set (gql, Gae)
Solution 1:
Reviving this question for completion:
The DISTINCT keyword has been introduced in release 1.7.4.
You can find the updated GQL reference (for example for Python) here.
Solution 2:
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': ''}
Solution 3:
One option would be to put the results into a set object:
http://www.python.org/doc/2.6/library/sets.html#sets.Set
The resulting set will consist only of the distinct values passed into it.
Failing that, building up a new list containing only the unique objects would work. Something like:
unique_results= []
for obj in user:if obj not in unique_results:unique_results.append(obj)
That for
loop can be condensed into a list comprehension as well.
Solution 4:
Sorry to dig this question up but in GAE I cannot compare objects like that, I must use .key() for comparison like that:
Beware, this is very inefficient :
def unique_result(array):
urk={} #unique results with key
for c in array:
if c.key() not in urwk:
urk[str(c.key())]=c
return urk.values()
If anyone has a better solution, please share.
Post a Comment for "Python: Distinct On Gquery Result Set (gql, Gae)"