Life is short, you need Python

Python - Dictionary's Key Value items

代码
>>> DictTest={"key1":"value1","key2":"value2","key3":"value3"}
>>> DictTest
{
'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
>>> DictTest.keys()
[
'key3', 'key2', 'key1']
>>> DictTest.values()
[
'value3', 'value2', 'value1']
>>> DictTest.items()
[(
'key3', 'value3'), ('key2', 'value2'), ('key1', 'value1')]

Notes:

  1. The keys method of a dictionary returns a list of all the keys. The list is not in the order in which the dictionary was defined (remember that elements in a dictionary are unordered), but it is a list.
  2. The values method returns a list of all the values. The list is in the same order as the list returned by keys, so params.values()[n] == params[params.keys()[n]] for all values of n.
  3. The items method returns a list of tuples of the form (key, value). The list contains all the data in the dictionary.
posted @ 2010-08-20 10:12  runfox545  阅读(549)  评论(0编辑  收藏  举报
白月黑羽 Python教程 白月黑羽Python