format_map()函数是Python中的内置函数,用于返回字典键的值。
用法:
string.format_map(z)
z是存储输入字典的变量,而string是输入字典的键。
参数:
input_dict:Takes a single parameter which is input dictionary.
返回值:
Returns key's values of the input dictionary.
代码1:
# input stored in variable a.
a = {'x':'John', 'y':'Wick'}
# Use of format_map() function
print("{x}'s last name is {y}".format_map(a))
输出:
John's last name is Wick
代码2:
# input stored in variable a.
a = {'x':"geeksforgeeks", 'y':'b'}
# Use of format_map() function
print('{x} {y}'.format_map(a))
输出:
geeksforgeeks b
代码3:
# Input dictionary
profession = { 'name':['Barry', 'Bruce'],
'profession':['Engineer', 'Doctor'],
'age':[30, 31] }
# Use of format_map() function
print('{name[0]} is an {profession[0]} and he'
' is {age[0]} years old.'.format_map(profession))
print('{name[1]} is an {profession[1]} and he'
' is {age[1]} years old.'.format_map(profession))
输出:
Barry is an Engineer and he is 30 years old. Bruce is an Doctor and he is 31 years old.
应用:
format_map()函数可以在许多实际应用中使用。
# Python code showing practical
# use of format_map() function
def chk_msg(n):
# input stored in variable a.
a = {'name':"George", 'mesg':n}
# use of format_map() function
print('{name} has {mesg} new messages'.format_map(a))
chk_msg(10)
输出:
George has 10 new messages
相关用法
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Python String | format_map()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。