Python:如何打印不带引号和方括号的列表

时间:2020-03-27 09:55:34

标签: python

我正在尝试以listvalue1value1的格式打印此Python value3

但是我得到的是这样的:

['value1', 'value2', 'value3']

所以我想摆脱'[]

这是我尝试过的方法,但是现在打印的结果没有仅括弧

tagList = []
count = 0
for i in range(20):
    try:
        if not response['TagList'][i]['Key']:
            print("Tags not found")
        else:
            if str(response['TagList'][i]['Key']) == "t_AppID":
                pass
            tagList.append(str(response['TagList'][i]['Key']))
    except:
        pass

return str(tagList)[1:-1]

2 个答案:

答案 0 :(得分:3)

join上使用str函数:

>>> mylist = ['value1', 'value2', 'value3']
>>> print(mylist)
['value1', 'value2', 'value3']
>>> print(', '.join(mylist))
value1, value2, value3

答案 1 :(得分:0)

要以字符串形式返回列表元素,可以使用类似以下内容的

def as_string(values):
    return ', '.join(values)

print(as_string(['a', 'b', 'c'])

输出:

a, b, c