“多重”;是";用Python
我有一个包含几百个对象的列表,我想检查是否有一个新对象已经添加到我的列表中(不是一个相等的对象,而是这个确切的实例) 我有这样一个愚蠢的认识:“多重”;是";用Python,python,Python,我有一个包含几百个对象的列表,我想检查是否有一个新对象已经添加到我的列表中(不是一个相等的对象,而是这个确切的实例) 我有这样一个愚蠢的认识: def is_one_of(test_object, all_objects): for elm in all_objects: if test_object is elm: return True return False 它不能更漂亮吗?使用any: if any(x is test_obje
def is_one_of(test_object, all_objects):
for elm in all_objects:
if test_object is elm:
return True
return False
它不能更漂亮吗?使用any
:
if any(x is test_object for x in all_objects):
看起来与您的代码非常相似:)使用any
:
if any(x is test_object for x in all_objects):
看起来与您的代码非常相似:)使用:
一旦找到True
结果,它将停止迭代生成器表达式。使用:
一旦找到
True
结果,它将停止对生成器表达式进行迭代。我想您正在运算符中查找。等效功能为:
def is_one_of(test_object, all_objects):
return test_object in all_objects
(但你真的不想把它写成函数)
编辑:我错了。根据该页面:
对于列表和元组类型,当且仅当存在索引i,使得x==y[i]为真时,y中的x为真
如果你的类没有定义\uuuuueq\uuuuu
,那就行了,但这比我想要依赖的更脆弱。例如:
class ObjWithEq(object):
def __init__(self, val):
self.val = val
def __eq__(self, other):
return self.val == other.val
a = ObjWithEq(1)
b = ObjWithEq(1)
assert a == b
assert a in [b]
class ObjWithoutEq(object):
def __init__(self, val):
self.val = val
a = ObjWithoutEq(1)
b = ObjWithoutEq(1)
assert a != b
assert a not in [b]
我想您正在寻找
中的。等效功能为:
def is_one_of(test_object, all_objects):
return test_object in all_objects
(但你真的不想把它写成函数)
编辑:我错了。根据该页面:
对于列表和元组类型,当且仅当存在索引i,使得x==y[i]为真时,y中的x为真
如果你的类没有定义\uuuuueq\uuuuu
,那就行了,但这比我想要依赖的更脆弱。例如:
class ObjWithEq(object):
def __init__(self, val):
self.val = val
def __eq__(self, other):
return self.val == other.val
a = ObjWithEq(1)
b = ObjWithEq(1)
assert a == b
assert a in [b]
class ObjWithoutEq(object):
def __init__(self, val):
self.val = val
a = ObjWithoutEq(1)
b = ObjWithoutEq(1)
assert a != b
assert a not in [b]
呃,我把id(element)放在一个集合里,
def _unit_list(self):
"""
Returns all units in the order they should be initialized.
(Performs search by width from start_point).
"""
unit_id_set = set()
unit_list = []
unit_id_set.add(self.start_point)
unit_list.append(self.start_point)
pos = 0
while pos < len(unit_list):
cur_unit = unit_list[pos]
for child in cur_unit.links_to:
if not (id(child) in unit_id_set):
unit_list.append(child)
unit_id_set.add(id(child))
pos += 1
return unit_list
def\u装置列表(自身):
"""
按初始化顺序返回所有单元。
(从起点开始按宽度执行搜索)。
"""
单位id集合=集合()
单位列表=[]
单元id设置添加(自起始点)
单元列表追加(自起始点)
pos=0
当pos
呃,我把id(元素)放在一个集合中:
def _unit_list(self):
"""
Returns all units in the order they should be initialized.
(Performs search by width from start_point).
"""
unit_id_set = set()
unit_list = []
unit_id_set.add(self.start_point)
unit_list.append(self.start_point)
pos = 0
while pos < len(unit_list):
cur_unit = unit_list[pos]
for child in cur_unit.links_to:
if not (id(child) in unit_id_set):
unit_list.append(child)
unit_id_set.add(id(child))
pos += 1
return unit_list
def\u装置列表(自身):
"""
按初始化顺序返回所有单元。
(从起点开始按宽度执行搜索)。
"""
单位id集合=集合()
单位列表=[]
单元id设置添加(自起始点)
单元列表追加(自起始点)
pos=0
当pos
您可以使用
if any(test_object is x for x in all_objects): ...
但是,如果您需要经常进行此测试,您可以保留一组所有对象id
s
all_ids = set(map(id, all_objects))
然后,您可以更快地使用
if id(test_object) in all_ids: ...
另一种可能适用的常见解决方案是,如果对象本身已被处理,则将其存储在特定字段中:
# add the object to the list
all_objects.append(x)
x.added = True
...
# Check if already added
if test_object.added: ...
你可以用
if any(test_object is x for x in all_objects): ...
但是,如果您需要经常进行此测试,您可以保留一组所有对象id
s
all_ids = set(map(id, all_objects))
然后,您可以更快地使用
if id(test_object) in all_ids: ...
另一种可能适用的常见解决方案是,如果对象本身已被处理,则将其存储在特定字段中:
# add the object to the list
all_objects.append(x)
x.added = True
...
# Check if already added
if test_object.added: ...
我在16秒之前就给了你;-)。2s-16s。最好是3个小时;-)没错,但如果你没打中,你的枪有多快都无关紧要。安维,我只是很兴奋,因为这些天我没有太多的改变成为FGITW——特别是对伟大的忍者马蒂恩·皮特斯!要小心那些兔子。。。他们肯定会使你的反应迟钝。你的反应迟钝了16秒;-)。2s-16s。最好是3个小时;-)没错,但如果你没打中,你的枪有多快都无关紧要。安维,我只是很兴奋,因为这些天我没有太多的改变成为FGITW——特别是对伟大的忍者马蒂恩·皮特斯!要小心那些兔子。。。它们肯定会使反应迟钝。不,因为
中的使用的是相等测试,而不是身份测试。@MartijnPieters这可能是真的,因为定义相等是正确的。没有\uuuu eq\uuu
的类的默认行为实际上是OP所要求的,但我不相信它。若后来的维护人员认为比较是一件好事并定义了它,那个么代码就会崩溃。尽管如此,它在默认情况下仍然可以工作。这是因为*default object.\uuuuuueq\uuuu`实现仅在对象相同时才返回True。但它仍然使用。\uuuuuueq\uuuuuu
@KirkStrauser——但OP已经指定了“不是一个相等的对象,而是这个实例”。FWIW尽管如此,我一直认为对于各种列表方法(.remove
,.index
,…)当它们真正的意思是“等于”时使用“是”这个词是有点遗憾的……不,因为中的使用的是相等测试,而不是标识。@MartijnPieters这可能是真的,因为如果定义了相等,它是正确的。没有\uuuu eq\uuu
的类的默认行为实际上是OP所要求的,但我不相信它。若后来的维护人员认为比较是一件好事并定义了它,那个么代码就会崩溃。尽管如此,它在默认情况下仍然可以工作。这是因为*default object.\uuuuuueq\uuuu`实现仅在对象相同时才返回True。但它仍然使用。\uuuuuueq\uuuuuu
@KirkStrauser——但OP已经指定了“不是一个相等的对象,而是这个实例”。尽管如此,我一直认为对于各种列表方法(.remove
,.index
,…)来说,当它们真正的意思是“等于”时使用“是”这个词……我不能让它比线性搜索更快吗?也许有某种方法可以强制Pythonset
使用id(…)
作为哈希函数