繁体   English   中英

python map function 与列表中的字典

[英]python map function with dictionaries in list

def card (Name,F_Name,Roll_No,Program):    

    x= f'''
    University Name
    
    
    Name: {Name}
    Father Name: {F_Name}
    Roll No: {Roll_No}
    Program: {Program}

    '''
    return x

data = [{'Name':'Student1','F_Name':"student's 1 father",'Roll_No':'123','Program':'BSCS'},{'Name':'student2','F_Name:':"student's 2 father",'Roll_No':'456','Program':'BSCS'},{'Name':'student3','F_Name':"student's 3 father",'Roll_No':'789','Program':'BSCS'}]

id_card = list(map(card,**data))

print (id_card)

I want to run this function using the map function and iterate the dictionary value in their respective positions in the user defined function.

map()不能传播字典本身。 **data要求data是字典,而不是字典列表。 在每次通话期间使用lambda传播字典。

id_card = list(map(lambda d: card(**d), data))

或使用列表理解:

id_card = [card(**d) for d in data]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM