簡體   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