Python字典

YYece 2018-07-14 11:46:39
# format函数通过字典设置参数
site = {"name": "Tom", "age": 10}
print("My name is {name}, {age} years old.".format(**site))
这里的 **site 是什么意思
...全文
311 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiemanR 2018-08-26
  • 打赏
  • 举报
回复
就是字典解包
lzw2016 2018-08-14
  • 打赏
  • 举报
回复
字典解包了解一下
欢乐的小猪 2018-08-14
  • 打赏
  • 举报
回复
字典
https://blog.csdn.net/hbu_pig/article/details/80268608
Gunlance 2018-08-14
  • 打赏
  • 举报
回复
你可以这样理解:print语句有两个占位,**site就把它所包含的内容按顺序传递,超出的就不传,主要看你的print有几个占位
fardeas 2018-07-15
  • 打赏
  • 举报
回复

person = {"who": "王尼玛", "age": 88}
"{who}今年已经{age}岁了".format(**person)
# 等价于
"{who}今年已经{age}岁了".format(who="王尼玛",age=88)

# format的其他用法
'{1}岁的{0},今天过{1}岁生日'.format('kzc', 18)
'{:>8}'.format('189')--> 189
'{:0>8}'.format('189')-->00000189
'{:a>8}'.format('189')-->aaaaa189
'{:.2f}'.format(321.33345)-->321.33
'{:b}'.format(17) # 二进制10001
'{:d}'.format(17) # 十进制17
'{:o}'.format(17) # 八进制21
'{:x}'.format(17) # 十六进制11
'{:,}'.format(1234567890) # 金额的千位分隔符1,234,567,890
FightingBoom 2018-07-15
  • 打赏
  • 举报
回复
这个地方应该不是用来存储冗余的值,参考这两个答案,
https://zhidao.baidu.com/question/2208074347500924508.html
https://zhidao.baidu.com/question/758033137588579084.html?qbl=relate_question_3
**site 应该是将 site 字典的值传递给 format ,以便前边语句对 name age 的调用~~~~
oyljerry 2018-07-14
  • 打赏
  • 举报
回复
**是形参中按照关键字传值把多余的传值以字典的方式呈现
方工 2018-07-14
  • 打赏
  • 举报
回复
未指明数量的关键词映射,可以是 dict, collections.defaultdict, collections.OrderedDict and collections.Counter.

37,717

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧