从数组中添加/删除对象 Python

请帮忙,在一个文件中,我有一个函数 initialize,其中包含一个数据对象数组。当用户输入 mediaitem 的参考号时,如何制作一个可以删除 mediaitem 的函数?另外,如何根据用户输入的内容添加媒体创意?


我需要添加/删除项目的功能是:


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!")


慕莱坞森
浏览 305回答 3
3回答

千万里不及你

你可以使用这个: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)    ]

慕桂英546537

您可以从列表中过滤项目并返回一个新列表,或者您可以找到项目的索引并通过引用将其从现有数组中删除。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)  

蛊毒传说

是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"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python