簡體   English   中英

如何編輯和刪除python字典中的鍵和值

[英]How to edit and delete keys and values in python dict

我有一個小問題需要解決,我必須編寫一個程序,將聯系人保存在字典中,並且能夠 1- 添加新聯系人 2- 刪除聯系人 3- 編輯聯系人 4- 列出聯系人 5- 顯示聯系人

我寫了一個簡單的程序,將聯系人保存到字典中,但我對 rest 有疑問,我真的需要一些幫助!!

這是我的代碼:

contacts = {"Mohamed": {"name": "Mohamed Sayed", "number": "017624857447", "birthday": "24.11.1996", "address": "Ginnheim 60487"},
            "Ahmed": {"name": "Ahmed Sayed", "number": "0123456789", "birthday": "06.06.1995", "address": "India"}}

def add_contact():

    for _ in range(0, 1):
        contact = {}
        name = input("Enter the name: ")
        number = input("Enter the number: ")
        birthday = input("Enter the birthday")
        address = input("Enter the address")


        contact["name"] = name
        contact["number"] = number
        contact["birthday"] = birthday
        contact["address"] = address
        print(contact)
        contacts.update(contact)


add_contact()
print(contacts)



def del_contact():
    user_input = input("Please enter the name of the contact you want to delete: ")
    for k in contacts:
        if user_input == contacts["name"]:
            del contacts[k]
del_contact()
print(contacts)


def edit_contact():
    user_input = input("please enter the contact you want to edit: ")
    for k, v in contacts:
        if user_input == contacts["name"]:
            contacts.update(user_input)


def list_contact():
    pass



def show_contact():
    user_input = input("please enter the contact you want to show: ")
    for k, v in contacts.items():
        if user_input == contacts["name"]:
            print(key, value)

show_contact()

對於您的def ,您不需要使用for... in range(...)循環,而是您可以通過user_value的值調用該值。 我決定不包含def edit_contact(): ,因為它目前不編輯任何東西,它所做的只是在考慮到這一點的情況下在contacts中添加一個新元素

contacts = {"Mohamed": {"name": "Mohamed Sayed", "number": "017624857447", "birthday": "24.11.1996", "address": "Ginnheim 60487"},
            "Ahmed": {"name": "Ahmed Sayed", "number": "0123456789", "birthday": "06.06.1995", "address": "India"}}

def add_contact():
    for _ in range(0, 1):
        contact = {}
        name = input("Enter the name: ")
        number = input("Enter the number: ")
        birthday = input("Enter the birthday")
        address = input("Enter the address")
        contact["name"] = name
        contact["number"] = number
        contact["birthday"] = birthday
        contact["address"] = address
        print(contact)
        contacts[name] = contact
add_contact()
print(contacts)



def del_contact(contacts):
    user_input = input("Please enter the name of the contact you want to delete: ")
    contacts = contacts.pop(user_input)
del_contact(contacts)
print(contacts)


def edit_contact():
    user_input = input("please enter the contact you want to edit: ")
    for k, v in contacts:
        if user_input == contacts["name"]:
            contacts.update(user_input)


def list_contact():
    pass

def show_contact():
    user_input = input("please enter the contact you want to show: ")
    print(contacts[user_input])
    #you can use 'contacts[user_input]['name', 'number', 'birthday' or 'address']' to call upon specific elements within the list

show_contact()

希望這可以幫助。

暫無
暫無

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

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