Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python字典_Python - Fatal编程技术网

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

我现在正在用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.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']
>>>