簡體   English   中英

從子元素與字符串匹配的列表中刪除元素

[英]Delete element from list where sub-element matches string

我有這個清單:

persons_list = [tom, alice]

湯姆和愛麗絲是Person兩個實例。 當我使用tom.sexe ,我得到的字符串是“ Male”,當我使用alice.sexe ,我得到的是她的sexe,即“女”。 我可能在person_list中有其他人實例。

現在,我想從persons_list中刪除一個元素,其中其中一個是sexe =“ Male”,但沒有成功。

到目前為止,我只創建了一個僅包含person_list的年齡的list(map(lambda x: x.age, persons_list))list(map(lambda x: x.age, persons_list)) ,但這與我想做的不同。

使用帶有過濾器的列表理解來重建列表:

persons_list = [person for person in persons_list if person.sexe != 'Male']

此挑選出屬於男性的所有對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM