繁体   English   中英

字符串在数组中返回奇怪的结果

[英]String is in array returning strange results

我正在创建一个 UUID 的动态数组,并且我有另一个现有 UUID 列表,我想从现有列表中删除不在新动态列表中的项目。 我正在尝试这样做

# Remove components that aren't being updated
new_component_id_for_existing_sections = []
for component in new_components:
    if component.get('section_holder_id'):
        new_component_id_for_existing_sections.append(component.get('component_id'))

    print('New component IDs')
    for com in new_component_id_for_existing_sections:
        print(com)

    print('Checking existing components')
    for existing_component in self.get_object().components.all():
        print(existing_component.component.id)
        print(existing_component.component.id not in new_component_id_for_existing_sections)

所以在这里我创建了数组new_component_id_for_existing_sections ,在我的示例中有两个 ID, self.get_object().components.all()有 3 个 ID。但是 output 给了我。

New component IDs
acae9374-d32d-4752-ba5a-9437a54dbbe7
a2a9d893-86ba-4d1d-938e-f638e7b2a4b2

Checking existing components
f3cb3cc6-4d66-4df5-8232-2c1f858c8632 <----Not in the array but returns that it is
True 

acae9374-d32d-4752-ba5a-9437a54dbbe7
True

a2a9d893-86ba-4d1d-938e-f638e7b2a4b2
True

第一项是说它在数组中,但不是,我不知道为什么

原来这是一个类型问题,existing_component.component.id 是一个 UUID,数组项是字符串,它不喜欢比较 UUID -> 字符串。

添加这个解决了问题existing_component.component.id.__str__()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM