簡體   English   中英

Python:如何打印沒有引號和方括號的列表

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

我正在嘗試以value1 , value1 , value3格式打印此 python list

但我得到的是這樣的:

['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]

str上使用join函數:

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

要將列表元素作為字符串返回,您可以使用以下內容:

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

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

輸出:

a, b, c

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM