1

我有以下字符串,我想打印列表对象:

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

我使用了以下 ode 但不起作用:

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

谢谢!

4

3 回答 3

4
>>> d = {u'categories': [u'Food', u'Grocery']}
>>> print "Categories: " + ', '.join(d['categories'])
Categories: Food, Grocery
>>> print "Categories: " + d['categories'][0]
Categories: Food
于 2013-04-22T07:57:06.037 回答
3

如果您想打印类别,然后是每个类别的名称,那么:

for category in your_dict['categories']:
    print 'Categories:', category
于 2013-04-22T07:57:09.203 回答
0
for k,v in your_dict.iteritems():
    for item in v:
        print k, ":", item
于 2013-04-22T08:08:49.627 回答