您需要将内部列表转换为元组,假设每个ObjectId('55119e14bf2e4e010d8b48f2')
是可散列的:
users_with_invites_ids_set = set(tuple(x) for x in users_with_invites_ids_list)
工作示例:
>>> class ObjectId(object):
... def __init__(self, v):
... self.v = v
...
>>> list_of_lists = [[ObjectId('55119e14bf2e4e010d8b48f2')], [ObjectId('54624128bf2e4e5e558b5a52')], [ObjectId('53a6e7bc763f4aa0308b4569')], [ObjectId('55241823bf2e4e59508b494c')]]
>>> set(tuple(x) for x in list_of_lists)
set([(<__main__.ObjectId object at 0x7f71483cfc50>,), (<__main__.ObjectId object at 0x7f71483cfd10>,), (<__main__.ObjectId object at 0x7f71483cfcd0>,), (<__main__.ObjectId object at 0x7f71483cfc90>,)])
如果您想创建一组ObjectId
独自一人,你可以这样做:
>>> set(x for lst in list_of_lists for x in lst)
set([<__main__.ObjectId object at 0x7f71483cfb10>, <__main__.ObjectId object at 0x7f71483db050>, <__main__.ObjectId object at 0x7f71483cfad0>, <__main__.ObjectId object at 0x7f71483cfd50>])