请帮助,在文件中我有一个函数初始化,其中包含一个数据对象数组。当用户输入中介项的参考编号时,如何制作一个可以删除中介项的函数?另外,如何根据用户输入的内容添加媒体创意?
我需要添加/删除项目的功能是:
def initialize():
medialist=[
MediaItem("TU2RL012","Movie","2001: A Space Odyssey",11.99, None ,"Stanley Kubrick","Keir Dullea"),
MediaItem("GV5N32M9","Book","A Brief History of Time",10.17,"Stephen Hawking", None, None),
MediaItem("1DB6HK3L","Movie","North by Northwest",8.99, None, "Alfred Hitchcock","Cary Grant"),
MediaItem("PO5T7Y89","Movie", "The Good, The Bad, The Ugly",9.99,None,"Sergio Leone", "Clint Eastwood"),
MediaItem("TR3FL0EW","Book","The Alchemist",6.99,"Paulo Coelho", None,None),
MediaItem("F2O9PIE9", "Book", "Thus Spoke Zarathustra",7.81, "Friedrich Nietzsche", None, None),
MediaItem("R399CED1","Book", "Jonathan Living Seagull",6.97,"Richard Bach", None, None),
MediaItem("2FG6B2N9","Movie", "Gone with the Wind",4.99, "Victor Fleming","Vivien Leigh", None),
MediaItem("6Y9OPL87","Book", "Gone with the Wind",7.99, "Margarett Mitchell", None, None)
]
return medialist
要添加一个项目,这是我到目前为止编写的代码:
def create_item():
x=input("Book or Movie?")
for obj in initialize():
if x== "Movie":
obj.media="Movie"
obj.title=input("Enter Movie Title:")
obj.reference=input("Enter Movie Reference:")
obj.price=input("Enter Movie Price:")
obj.director=input("Enter Director Name:")
obj.lead_actor=input("Enter Lead Actor Name:")
break
if x== "Book":
obj.media="Book"
obj.title=input("Enter Book Title:")
obj.reference=input("Enter Book Reference:")
obj.price=input("Enter Book Price:")
obj.author=input("Enter Author Name:")
break
else:
print("Wrong Input!")
答案 0 :(得分:0)
您可以从列表中过滤出该项目并返回一个新列表,或者可以找到该项目的索引,并通过引用将其从现有数组中删除。
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)
答案 1 :(得分:0)
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"
答案 2 :(得分:0)
您可以使用此功能:
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)
]