Python字典
我现在正在用python编写字典,我正在尝试访问Python字典,python,Python,我现在正在用python编写字典,我正在尝试访问profile.keys和profile.items命令。不过,所有发生的事情都会出现类似于的情况。我应该怎么做?如果要在键上迭代: for x in profile.keys(): print x 或价值观 for x in profile.values(): print x 或键值对: for x, y in profile.items(): print x, y 或者只指定一个变量供以后使用 x = profile.item
profile.keys
和profile.items
命令。不过,所有发生的事情都会出现类似于
的情况。我应该怎么做?如果要在键上迭代:
for x in profile.keys():
print x
或价值观
for x in profile.values():
print x
或键值对:
for x, y in profile.items():
print x, y
或者只指定一个变量供以后使用
x = profile.items()
等等。您必须这样调用
profile.keys()
和profile.items()
。它们是方法
如果省略括号,则实际上不会调用该函数。
取而代之的是对函数对象的引用
mydict={'foo':'bar'}
>>>mydict.keys
>>>mydict.keys()
['foo']
>>>
您能发布返回此信息的代码吗?
>>> mydict = {'foo':'bar'}
>>> mydict.keys
<built-in method keys of dict object at 0x01982540>
>>> mydict.keys()
['foo']
>>>