如何在python中打印字典的键值对?

时间:2022-06-30 07:37:38

I want to output my key value pairs from a python dictionary as such:

我想从python字典中输出我的键值对:

key1 \t value1
key2 \t value2

I thought I could maybe do it like this:

我想我可以这样做:

for i in d:
    print d.keys(i), d.values(i)

but obviously that's not how it goes as the keys() and values() don't take an argument...

但很明显,这不是键()和值()的方式,而不是参数。

Thanks.

谢谢。

8 个解决方案

#1


138  

Your existing code just needs a little tweak. i is the key, so you would just need to use it:

您现有的代码只需稍加调整即可。我是关键,所以你只需要使用它:

for i in d:
    print i, d[i]

You can also get an iterator that contains both keys and values. In Python 2, d.items() returns a list of (key, value) tuples, while d.iteritems() returns an iterator that provides the same:

您还可以得到包含键值和值的迭代器。在Python 2中,.items()返回一个(键、值)元组的列表,而d.iteritems()返回一个迭代器,它提供相同的:

for k, v in d.iteritems():
    print k, v

In Python 3, d.items() returns the iterator; to get a list, you need to pass the iterator to list() yourself.

在Python 3中,.items()返回迭代器;要获得一个列表,您需要传递迭代器来列出()自己。

for k, v in d.items():
    print(k, v)

#2


28  

A little intro to dictionary

一本字典的介绍。

d={'a':'apple','b':'ball'}
d.keys()  # displays all keys in list
['a','b']
d.values() # displays you values in list
['apple','ball']
d.items() # displays you pair tuple of key and value
[('a','apple'),('b','ball')

print keys,values method one

打印键,值的方法

for x in d.keys():
    print x +" => " + d[x]

Another method

另一种方法

for key,value in d.items():
    print key + " => " + value

You can get keys using iter

你可以使用iter获取密钥。

>>> list(iter(d))
['a', 'b']

you can get value of key of dictionary using get(key, [value]):

您可以使用get(key, [value])获取字典的键值:

d.get('a')
'apple'

if key is not present in dictionary,when default value give, will return value.

如果关键字不在字典中,当默认值给出时,将返回值。

d.get('c', 'Cat')
'Cat'

#3


9  

Or, for Python 3:

或者,对于Python 3:

for k,v in dict.items():
    print(k, v)

#4


2  

for key, value in d.iteritems():
    print key, '\t', value

#5


1  

>>> d={'a':1,'b':2,'c':3}
>>> for kv in d.items():
...     print kv[0],'\t',kv[1]
... 
a   1
c   3
b   2

#6


1  

If you want to sort the output by dict key you can use the collection package.

如果您想通过dict键对输出进行排序,可以使用集合包。

import collections
for k, v in collections.OrderedDict(sorted(d.items())).items():
    print(k, v)

It works on python 3

它在python3上运行。

#7


0  

In addition to ways already mentioned.. can use 'viewitems', 'viewkeys', 'viewvalues'

除了已经提到的方法。可以使用“viewitems”、“viewkeys”、“viewvalues”

>>> d = {320: 1, 321: 0, 322: 3}
>>> list(d.viewitems())
[(320, 1), (321, 0), (322, 3)]
>>> list(d.viewkeys())
[320, 321, 322]
>>> list(d.viewvalues())
[1, 0, 3]

Or

>>> list(d.iteritems())
[(320, 1), (321, 0), (322, 3)]
>>> list(d.iterkeys())
[320, 321, 322]
>>> list(d.itervalues())
[1, 0, 3]

or using itemgetter

或者使用itemgetter

>>> from operator import itemgetter
>>> map(itemgetter(0), dd.items())     ####  for keys
['323', '332']
>>> map(itemgetter(1), dd.items())     ####  for values
['3323', 232]

#8


0  

You can access your keys and/or values by calling items() on your dictionary.

您可以通过在字典上调用items()来访问您的键和/或值。

for key, value in d.iteritems():
    print(key, value)

#1


138  

Your existing code just needs a little tweak. i is the key, so you would just need to use it:

您现有的代码只需稍加调整即可。我是关键,所以你只需要使用它:

for i in d:
    print i, d[i]

You can also get an iterator that contains both keys and values. In Python 2, d.items() returns a list of (key, value) tuples, while d.iteritems() returns an iterator that provides the same:

您还可以得到包含键值和值的迭代器。在Python 2中,.items()返回一个(键、值)元组的列表,而d.iteritems()返回一个迭代器,它提供相同的:

for k, v in d.iteritems():
    print k, v

In Python 3, d.items() returns the iterator; to get a list, you need to pass the iterator to list() yourself.

在Python 3中,.items()返回迭代器;要获得一个列表,您需要传递迭代器来列出()自己。

for k, v in d.items():
    print(k, v)

#2


28  

A little intro to dictionary

一本字典的介绍。

d={'a':'apple','b':'ball'}
d.keys()  # displays all keys in list
['a','b']
d.values() # displays you values in list
['apple','ball']
d.items() # displays you pair tuple of key and value
[('a','apple'),('b','ball')

print keys,values method one

打印键,值的方法

for x in d.keys():
    print x +" => " + d[x]

Another method

另一种方法

for key,value in d.items():
    print key + " => " + value

You can get keys using iter

你可以使用iter获取密钥。

>>> list(iter(d))
['a', 'b']

you can get value of key of dictionary using get(key, [value]):

您可以使用get(key, [value])获取字典的键值:

d.get('a')
'apple'

if key is not present in dictionary,when default value give, will return value.

如果关键字不在字典中,当默认值给出时,将返回值。

d.get('c', 'Cat')
'Cat'

#3


9  

Or, for Python 3:

或者,对于Python 3:

for k,v in dict.items():
    print(k, v)

#4


2  

for key, value in d.iteritems():
    print key, '\t', value

#5


1  

>>> d={'a':1,'b':2,'c':3}
>>> for kv in d.items():
...     print kv[0],'\t',kv[1]
... 
a   1
c   3
b   2

#6


1  

If you want to sort the output by dict key you can use the collection package.

如果您想通过dict键对输出进行排序,可以使用集合包。

import collections
for k, v in collections.OrderedDict(sorted(d.items())).items():
    print(k, v)

It works on python 3

它在python3上运行。

#7


0  

In addition to ways already mentioned.. can use 'viewitems', 'viewkeys', 'viewvalues'

除了已经提到的方法。可以使用“viewitems”、“viewkeys”、“viewvalues”

>>> d = {320: 1, 321: 0, 322: 3}
>>> list(d.viewitems())
[(320, 1), (321, 0), (322, 3)]
>>> list(d.viewkeys())
[320, 321, 322]
>>> list(d.viewvalues())
[1, 0, 3]

Or

>>> list(d.iteritems())
[(320, 1), (321, 0), (322, 3)]
>>> list(d.iterkeys())
[320, 321, 322]
>>> list(d.itervalues())
[1, 0, 3]

or using itemgetter

或者使用itemgetter

>>> from operator import itemgetter
>>> map(itemgetter(0), dd.items())     ####  for keys
['323', '332']
>>> map(itemgetter(1), dd.items())     ####  for values
['3323', 232]

#8


0  

You can access your keys and/or values by calling items() on your dictionary.

您可以通过在字典上调用items()来访问您的键和/或值。

for key, value in d.iteritems():
    print(key, value)