我應該刪除列表中項目的單引號或雙引號。我的清單是lis=['greaterthan','lessthan']res='[%s]' % ', '.join(map(str,lis))print(res) #[greaterthan,lessthan]print(type(res)) #<class 'str'> 即使在刪除引號后我也需要列表類型。我嘗試了很多方法可以刪除引號但它以字符串格式顯示,但我需要列表類型。誰能建議。
3 回答

慕容森
TA貢獻1853條經驗 獲得超18個贊
嘗試這個 :
lis=['greaterthan,lessthan']
res = lis[0].split(",")
print(res) #['greaterthan', 'lessthan']
print(type(res)) #<class 'list'>

江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
要解決這個問題:'我只需要 [greaterthan,lessthan] #<class 'list'>'
如果您“刪除”字符串周圍的引號,您將獲得一個名稱(對對象的引用)。因此你可以這樣做:
greaterthan = object()
lessthan = object()
lst = [greaterthan, lessthan]
添加回答
舉報
0/150
提交
取消