无法以自定义方式解压缩平面清单

时间:2019-11-27 18:46:40

标签: python python-3.x list tuples

我已经能够像下面这样使用python制作一个平面列表来拆开元组列表:

{
    "username": "bob murrey",
    "id": 200
}

输出:

items = [('Brandon', 'WME', 'phone'), ('Brian', 'Endeavor', 'phone one'), ('Duncan', 'Entertainment', 'phone two')]
flat_list =  [i for elem in items for i in elem]
print(flat_list)

如果我使用像['Brandon', 'WME', 'phone', 'Brian', 'Endeavor', 'phone one', 'Duncan', 'Entertainment', 'phone two'] 这样的星号,列表将变为:

print(*flat_list)

如何使列表如下所示?

Brandon WME phone Brian Endeavor phone one Duncan Entertainment phone two

4 个答案:

答案 0 :(得分:1)

使用string.join。示例:

print(", ".join(["a", "b", "c"]))

答案 1 :(得分:1)

您要使用定界符https://github.com/gearman/gearmand/issues/63字符串:

>>> ", ".join(flat_list)
'Brandon, WME, phone, Brian, Endeavor, phone one, Duncan, Entertainment, phone two'

答案 2 :(得分:1)

只需更改printsep参数:

items = [('Brandon', 'WME', 'phone'), ('Brian', 'Endeavor', 'phone one'), ('Duncan', 'Entertainment', 'phone two')]
flat_list = [i for elem in items for i in elem]
print(*flat_list, sep=", ")

输出

Brandon, WME, phone, Brian, Endeavor, phone one, Duncan, Entertainment, phone two

答案 3 :(得分:0)

您可能不确定使用np.flatten()。