Python 问题 - 我有一个类列表,如何删除重复项?

标签 python google-app-engine

正在编写一个 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/

相关文章:

python - Google App Engine Python 中使用区域设置的货币格式

python - 套接字错误 : [Errno 111] when trying to connect to a socket

c++ - "for num in [1,4,5]"C++ 中的 Python 等价物?

google-app-engine - Google Code 与 Google App Engine?

firebase - 如何传递应用程序功能?

reactjs - 如何使用代理将 Flask API 和 React 前端微服务部署到 Google App Engine?

python - Zeromq:为什么经销商发送一条消息并从一个客户接收消息?

python - 将一个数组的每个元素除以另一个数组的每个元素

python - SQLAlchemy InvalidRequestError : failed to locate name happens only on gunicorn

java - GAE哪个更适合全局系统内容?