我有以下字符串,我想打印列表对象:
{u'categories': [u'Food', u'Grocery']}
我使用了以下颂歌但不起作用:
d = {u'categories': [u'Food', u'Grocery']}
print "Categories: " + d['categories']
print "Categories: " + d['categories[0]']
谢谢!
答案 0 :(得分:4)
>>> d = {u'categories': [u'Food', u'Grocery']}
>>> print "Categories: " + ', '.join(d['categories'])
Categories: Food, Grocery
>>> print "Categories: " + d['categories'][0]
Categories: Food
答案 1 :(得分:3)
如果您想要打印类别,然后是每个类别的名称,那么:
for category in your_dict['categories']:
print 'Categories:', category
答案 2 :(得分:0)
for k,v in your_dict.iteritems():
for item in v:
print k, ":", item