[英]Why will the output be 11032? Python3.6
d = {10:"x", 1:"wx", 2:"yz"}
a = d.setdefault(1)
b = d.setdefault(3)
s = "{}" * len(d)
print(s.format(*d))
Why will the output be 11032? 为什么输出为11032?
After 2 setdefault calls, 经过2次setdefault调用后,
d = {10: "x", 1: "wx", 2: "yz"}
d.setdefault(1) # does not change the dictionary because there's already 1
d.setdefault(3) # add 3 with value None (default if not speicfied)
d
becomes: d
变为:
>>> d
{10: 'x', 1: 'wx', 2: 'yz', 3: None}
Iterating dictionary yields dictionary keys: 10, 1, 2, 3
. 迭代字典收率字典键: 10, 1, 2, 3
。 (iteration performed by *d
to unpack the argument d
): (由*d
执行的迭代将参数d
解压缩):
>>> for key in d:
... print(key)
...
10
1
2
3
So, s.format(*d)
is equivalent to '{}{}{}{}'.format(10, 1, 2, 3)
. 因此, s.format(*d)
等同于'{}{}{}{}'.format(10, 1, 2, 3)
s.format(*d)
'{}{}{}{}'.format(10, 1, 2, 3)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.