正在编写一个 App Engine 应用程序(这是一个简单的游戏任务系统):
所以我有一个列表
class Quest(db.Model):
name = db.StringProperty()
# note: I made about 10 different quest entities ( quest1 to quest10)
class User(db.Model):
completed_quests = db.StringListProperty() # to store keys of completed quests
# note: I made some fake data showing that the user completed 3 quests.
# user.completed_quests = ["key1","key2","key3"] - keys belong to the corresponding quests
所以我查询用户和他/她完成的任务。 user = User.get_by_key_name(userid)
然后我查询任务模型 all_quests = Quest.all()
问题是:如何交叉检查我的 user.completed_quests 列表和 all_quests ?
我的目标:我想向用户展示一个网页,他/她可以在其中看到: - 已完成任务的列表以及 - 未完成的任务。
我正在使用的方法:
# prepare a buffer
completed_quests = []
for quest in all_quests:
for k,completed_quest in enumerate(user.completed_quests):
if str(completed_quest) == str(quest.key()): # the point of detection
completed_quests.append(completed_quest)
# final product is a list of completed quest entites
但是我该如何为我未完成的任务做到这一点呢?
最佳答案
您可以使用差异
:
all = set(quest.key() for quest in all_quests)
complete = set(completed_quests)
incomplete = all.difference(complete)
关于Python 问题 - 我有一个类列表,如何删除重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2227297/