+ -
当前位置:首页 → 问答吧 → python字典中使用字符串格式符的问题

python字典中使用字符串格式符的问题

时间:2010-10-25

来源:互联网

在交互式解释器中执行下面语句
Python code

tt={1: 'abc', 2: 'cde', 3: 'fgh'}
print "%(1)s ,%(3)s" % tt



报错:
Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
  print "%(1)s ,%(3)s" % tt
KeyError: '1'

如果改用字符串做键则是正常的,不明白数字做Key,输出为什么报错?

作者: Dic4000   发布时间: 2010-10-25

好像是有这个限制不让用数字做key。

作者: codesnail   发布时间: 2010-10-25