簡體   English   中英

如何用雙引號列表轉換單引號列表

[英]how to convert single quotes list with double quotes list

我有一個帶單引號的預定義列表,我想在每個元素中使用雙引號。

例如,這是我的預定義列表

l = ['A','b']

我只需要作為列表的輸出

l = ["A","b"]

我正在嘗試使用 json,但它以字符串形式提供列表,但我想要列表。

import json
l = ['A','b']
output = json.dumps(l)
print(type(output))

你的情況沒有區別。 嘗試打印l

l_double = ["A","b"]

l_single = ['A','b']

print(l_double)

print(l_single)

返回

['A', 'b']
['A', 'b']

如果你真的想在你的列表項周圍加雙引號,試試這樣的:

l = ['A','b']

l_real_double = [f'"{c}"' for c in l]

print(l_real_double)

哪個打印

['"A"', '"b"']
L = ['A', 'B', 'C']
print('[', end='') #just to print the opening third bracket [
for i in range(len(L)): #the variable i will assume the values from 0 to length of L - 1 i.e. the indices of the elements
    print('"' + L[i] + '"', end='') #display a double quote and the string inside it
    if i < len(L)-1:
        print(',', end=' ') #if i is not the index of the last element then display a comma after it
print(']') #just to print the closing third bracket ]

暫無
暫無

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

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