从python中的元组列表返回包含2个键的字典

从python中的元组列表返回包含2个键的字典,python,function,tuples,Python,Function,Tuples,我有一个元组列表,看起来像这样 people=[('John',36,'M'),('Rachel',24,'F'),('Deardrie',78,'F'),('Ahmed',17,'M'),('Sienna',14,'F')] 我正在尝试创建一个函数。其中有2个参数功能(人员、姓名) 函数应该返回一个带有两个键的字典,“age”和“gender”,其中的值是元组中的值,元组包含作为第二个参数传递给函数的名称。如果在元组列表中找不到该名称,则返回“None” 我正在努力创建一个函数,因为我似乎找不

我有一个元组列表,看起来像这样

people=[('John',36,'M'),('Rachel',24,'F'),('Deardrie',78,'F'),('Ahmed',17,'M'),('Sienna',14,'F')]

我正在尝试创建一个函数。其中有2个参数功能(人员、姓名) 函数应该返回一个带有两个键的字典,“age”和“gender”,其中的值是元组中的值,元组包含作为第二个参数传递给函数的名称。如果在元组列表中找不到该名称,则返回“None”

我正在努力创建一个函数,因为我似乎找不到任何关于如何处理3元素元组列表的信息

有什么建议吗?

类似这样的建议吗

people1 = [('John', 36, 'M'), ('Rachel', 24, 'F'), ('Deardrie', 78, 'F'), ('Ahmed', 17, 'M'), ('Sienna', 14, 'F')]

def xyz(people, name):
    found={}
    for _name,_age,_gender in people:
        if _name==name:
            found["Age"]=_age
            found["Gender"]=_gender
            return found
    return None
    
print(xyz(people1,"John"))
print(xyz(people1,"John1"))
我的输出:

{'Age': 24, 'Gender': 'F'}

None

下面是一个返回字典列表的函数。如果有多个元组具有相同的名称,则函数会将列表中的所有元组作为字典返回。如果没有任何具有特定名称的元组,则返回
None

def my_f(people, name):
result = []
for each in people:
    if each[0] == name:
        result.append({'age': each[1], 'gender': each[2]})
if len(result) == 0:
    return None
return result

你想在你的字典里为每一个可能的性别、年龄对写一个条目吗?例如,你的名单上没有33,M的人。这本新字典也应该有33,M吗?
{'Age': 24, 'Gender': 'F'}

None
def my_f(people, name):
result = []
for each in people:
    if each[0] == name:
        result.append({'age': each[1], 'gender': each[2]})
if len(result) == 0:
    return None
return result