将此列表转换为字符串的最有效方法是什么?

时间:2015-04-29 03:15:55

标签: python-2.7 dictionary nested-lists

我正在尝试创建一个由字典值中的元素组成的字符串。

For line in dictionary:
    print 'Category'.format(line['categories'])

这是运行上面代码的结果:

Category = [[u'Cafes', u'cafes'], [u'Pakistani', u'pakistani'], [u'Halal', u'halal']]
Category = [u'American (New)', u'newamerican'], [u'Desserts', u'desserts']]

当我使用它时:

for x in line['categories']:
    cat =', '.join(x)
    print cat

结果如下:

Cafes, cafes
Pakistani, pakistani
Halal, halal

American (New), newamerican
Desserts , desserts

我想要实现的目标是:

'Cafes, cafes, Pakistani, pakistani, Halal, halal'
American (New), newamerican, Desserts , desserts

1 个答案:

答案 0 :(得分:0)

你可以这样做:

ss = ''
for x in line['categories']:
    cat =', '.join(x)
    ss += cat

print ss