3 回答
TA贡献1784条经验 获得超9个赞
你可以使用这个:
def delete(item_num, List):
List = [item for item in List if item_num != item[0] ]
return List
例如:
mylist = delete("GV5N32M9",initialize())
print(mylist)
结果:
[
("TU2RL012","Movie","2001: A Space Odyssey",11.99, None ,"Stanley Kubrick","Keir Dullea"),
("1DB6HK3L","Movie","North by Northwest",8.99, None, "Alfred Hitchcock","Cary Grant"),
("PO5T7Y89","Movie", "The Good, The Bad, The Ugly",9.99,None,"Sergio Leone", "Clint Eastwood"),
("TR3FL0EW","Book","The Alchemist",6.99,"Paulo Coelho", None,None),
("F2O9PIE9", "Book", "Thus Spoke Zarathustra",7.81, "Friedrich Nietzsche", None, None),
("R399CED1","Book", "Jonathan Living Seagull",6.97,"Richard Bach", None, None),
("2FG6B2N9","Movie", "Gone with the Wind",4.99, "Victor Fleming","Vivien Leigh", None),
("6Y9OPL87","Book", "Gone with the Wind",7.99, "Margarett Mitchell", None, None)
]
TA贡献1848条经验 获得超10个赞
您可以从列表中过滤项目并返回一个新列表,或者您可以找到项目的索引并通过引用将其从现有数组中删除。
def remove(mid, media_list):
# option 1: filter it out
# does not remove by reference, returns new array
return list(filter(lambda item: item.mid != mid))
# option 2: remove by reference
# removes by reference, does not return new array
for i in range(len(media_list)):
if media_list[i].mid == mid:
media_list.pop(i)
TA贡献1895条经验 获得超3个赞
是MediaItem一个班吗?如果是,则数组中的项是指向该类对象的指针。如果您需要保持这种方式,那么您将需要编写一个搜索方法来根据数组中的键查找特定对象。
假设您有 MediaItems 的方法,例如 MediaItem.getKey(),您可以执行以下操作...
def delete_item(self, key):
for i in range(len(medialist)):
if medialist[i].getKey() == key:
del(l[i])
return "success"
return "item not found"
添加回答
举报