Python: DISTINCT on GQuery result set (GQL, GAE)

前端 未结 4 799
南方客
南方客 2020-12-10 16:34

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:<

4条回答
  •  悲&欢浪女
    2020-12-10 17:10

    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.

提交回复
热议问题