简体   繁体   English

Python:如何打印没有引号和方括号的列表

[英]Python: How to print a list without quotations and square brackets

I am trying to print this python list as in the format value1 , value1 , value3 .我正在尝试以value1 , value1 , value3格式打印此 python list

But what I get is something like this:但我得到的是这样的:

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

So I want to get rid of ' and [] .所以我想摆脱'[]

This is what I tried, but it now prints the result without only brackets :这是我尝试过的,但它现在打印没有括号的结果:

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]

Use the join function on str :str上使用join函数:

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

To return list elements as a string you can use something like:要将列表元素作为字符串返回,您可以使用以下内容:

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

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

Output:输出:

a, b, c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM