首页 > 解决方案 > 如何从字符串中删除方括号?

问题描述

我想删除代码输出中的两个方括号。

我的代码:

request2 = requests.get('https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId=1045618&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'})
json2 = request2.json()
for selection in json2['selections']:
    for fluc in selection['flucs'][0]:
        flucs1 = ast.literal_eval(selection['flucs'])
        flucs2 = flucs1[-2:]
        flucs3 = [[x[1]] for x in flucs2]

代码示例输出:

[[12.97], [13.13]]

所需的代码输出:

12.97, 13.13

标签: pythonstring

解决方案


.join()也有助于像这样加入列表列表:

output = [[12.97], [13.13]]
result = '\n'.join(','.join(map(str, row)) for row in output)
print(result)

输出 :

12.97
13.13

也试试这个:

result2 = ', '.join(','.join(map(str, row)) for row in output)
print(result2)

输出:

 12.97, 13.13

推荐阅读