2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
.format(**bdays)相當(dāng)于.format(key1=value, key2=value2,...)鍵是名稱,值是生日。
因此,要使其發(fā)揮作用,您的打印聲明需要成為 -
print('\n {Wesley Neill} \n {Victoria Neill} \n {Heather Neill}'.format(**bdays))
這將打印這 3 個(gè)人的生日。
在您的 python 控制臺(tái)中嘗試以下操作 -
>>> [*bdays]
['Wesley Neill', 'Victoria Neill', 'Heather Neill']

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
首先星號(hào)符號(hào)的作用:
**dict is equivalent to k1=v1, k2=v, ...
*dict is equivalent to [k1, k2, ...]
所以你在做:
# This print('{first} {last}'.format(**data)) is:
print('{first} {last}'.format(first='Hodor', last='Hodor!'))
# This print('\n {} \n {} \n {}'.format(*bdays)) is:
print('\n {} \n {} \n {}'.format(['Wesley Neill', 'Victoria Neill', 'Heather Neill']))
# This print('\n {} \n {} \n {}'.format(**bdays)) is:
print('\n {} \n {} \n {}'.format('Wesley Neill'='January 6, 1985', 'Victoria Neill'='August 25, 1992', 'Heather Neill'='June 25, 1964'))
最終格式字符串中沒有說(shuō)明任何鍵,因此您會(huì)收到錯(cuò)誤消息。
添加回答
舉報(bào)