简体   繁体   English

如何在Python中的字典中打印列表?

[英]How can I print list in dictionary in Python?

I have following string and I want to print the list objects: 我有以下字符串,我想打印列表对象:

{u'categories': [u'Food', u'Grocery']}

I used following ode but does not work: 我用下面的颂歌,但不起作用:

d = {u'categories': [u'Food', u'Grocery']}                        
print "Categories: " + d['categories']                     
print "Categories: " + d['categories[0]']

Thank you! 谢谢!

>>> d = {u'categories': [u'Food', u'Grocery']}
>>> print "Categories: " + ', '.join(d['categories'])
Categories: Food, Grocery
>>> print "Categories: " + d['categories'][0]
Categories: Food

If you're wanting to print categories followed by the name of each category then: 如果要打印类别,然后打印每个类别的名称,则:

for category in your_dict['categories']:
    print 'Categories:', category
for k,v in your_dict.iteritems():
    for item in v:
        print k, ":", item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-如何从列表中的许多词典中打印某个词典字段? - Python - How can i print a certain dictionary field from many dictionaries within a list? 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? 如何在 tkinter 的列表框中打印字典列表 - How can i print a dictionary list in a listbox from tkinter 如何在python列表中打印此格式 - How can I print this format in the list of python 如何在 Python 中打印字典键值的平均值? - How can I print the mean of key values of a dictionary in Python? 如何将 Python 字典打印为按键深度排序的 YAML - How can I print a Python dictionary as YAML sorted by key depth 如果它们是基于字典的,如何在Python中打印变量的值? - How can I print the value of variables in Python if they are dictionary-based? 如何在 Python 的 GUI 中打印字典中的所有值 - How can i print all values from a dictionary in a GUI in Python 如何更改在 Python 中排序的嵌套字典的打印格式 - How Can I Change The Print Format Of A Nested Dictionary That Is Sorted In Python Python 中的 Discord ChatBot:如何从字典中打印某些内容? - Discord ChatBot in python: How can I print something from a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM