Python 元组名称和编号列表作为参数并删除

Python 元组名称和编号列表作为参数并删除,python,function,tuples,Python,Function,Tuples,定义一个名为update\u contact(元组列表、搜索名称、新手机)的函数,该函数将元组列表、名称-search\u name和电话号码-新手机作为参数。如果列表中存在姓名,该函数将通过删除现有元组并使用参数名称和电话号码向列表末尾添加新元组来更新列表中的现有联系人元组。该功能还应打印一条消息,指示联系人已更改 注意:如果参数名称不存在,则函数应 print ("xxx is not found.") 其中xxx是参数名 注意:您可以假定联系人列表仅包含唯一的联系人

定义一个名为
update\u contact(元组列表、搜索名称、新手机)
的函数,该函数将
元组列表
、名称-
search\u name
和电话号码-
新手机
作为参数。如果列表中存在姓名,该函数将通过删除现有元组并使用参数名称和电话号码向列表末尾添加新元组来更新列表中的现有联系人元组。该功能还应打印一条消息,指示联系人已更改

注意:如果参数名称不存在,则函数应

print ("xxx is not found.")
其中xxx是参数名

注意:您可以假定联系人列表仅包含唯一的联系人


我正在学习元组,这真的很让人困惑,我如何处理这类问题?

好的,让我们从定义元组开始:

在您的例子中,您需要一个元组列表;让我们从定义它开始

''' This is a list that contains 3 tuples and each tuple contain the contact name and 
     the phone number'''
contact_list = [('aaa', 123), ('bbb', 456), ('ccc', 789)]
功能
update\u contact
的定义如下:

# Takes as parameters the list, the name to search for, and the new phone number
def update_contact(contact_list, search_name, new_phone): 
    '''
      Looping through the list (each iteration will give a tuple)
      - first iteration: i = ('aaa', 123)
      - second iteration: i = ('bbb', 456)
    '''
    for i in contact_list:
        '''         
          In order to get the name inside the tuple (Assuming that the name comes first 
          before the number) we access it using i[0]; 
          If we take the first iteration: i = ('aaa', 123), i[0] = 'aaa'
          and we compare it to search_name
        '''
        if i[0] == search_name:
            # If it matches we need to remove that tuple from the list 
            contact_list.remove(i)
            # And append the new tuple to that list
            contact_list.append((search_name, new_phone))
            # then return the new list
            return (contact_list)
    # return if search_name was not found in list
    return search_name + " Does not exist"
最后调用函数:

contact_list = update_contact(contact_list, 'bbb', 111)
print(contact_list)

根据您的问题,我们需要包含一个人的
姓名
电话号码
的元组列表。我们需要定义
update
函数。对于这个问题,
dictionary
将是更好的方法(Name作为key,Phone number作为value)

def update_contact(contacts,nm,phno):
    flag=0
    for c in contacts:
        if c[0]==nm:
            #to remove object from list
            contacts.remove(c)
            flag=1
    if flag==1:
        new=(nm,phno)
        #to append new tuple to list
        contacts.append(new)
    else:
        print("Name not found!!")
        
    
    return contacts

#existing list of tuples
contacts=[("Rahul","9111111111"),("Taylor","9112222211")]
print("Contacts:",contacts)
nm=input("Enter name to search :")
phno=int(input("Enter new phone number :"))
#calling function
print(" Updated Contacts: ",update_contact(contacts,nm,phno))

似乎使用字典来构建数据结构更简单、更快

def update_contact(list_of_tuples, search_name, new_phone):
    contact_dict = dict(list_of_tuples)
    if search_name in contact_dict:
        print(f"{search_name} is updated.")
    else:
        print(f"{search_name} is not found.")
    contact_dict.update(search_name=new_phone)
    return [(k, v) for k, v in contact_dict.items()]
新插入的联系人信息实际上并不在末尾,因为字典使用散列来存储数据

我认为这个函数应该只使用dict而不是tuple列表

def update_contact(contact_dict, search_name, new_phone):
    if search_name in contact_dict:
        print(f"{search_name} is updated.")
    else:
        print(f"{search_name} is not found.")
    contact_dict.update(search_name=new_phone)
    return contact_dict

请查收。到底是什么让人困惑?您尝试过什么?请用您编写的代码更新您的问题,并准确地告诉我们哪一位是令人困惑的。请不要不劳而获地向人们提供答案。事实并非如此。