首页 > 解决方案 > 收集列表中的单个变量

问题描述

list = [ "test1", "test2", "test3"]

我想将列表中的变量作为字符串分配给 x,每个新变量都会放在一起,但不会放在最后。我想"test, test2, test3"

我该怎么办?

标签: pythonpython-2.7

解决方案


试试这个。只需添加列表中的所有字符串。还要避免使用关键字变量名称,例如list.

output= ""
for item in myList:
    if output == "":
        output += item
    else:
        output  += ', '
        output += item

print(str)

推荐阅读