本文介紹了將表示列表的字符串轉換為實際的列表對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個代表列表的字符串:
I have a string that represents a list:
"[22, 33, 36, 41, 46, 49, 56, 72, 85, 92, 95, 98, 107, 118, 120, 123, 124, 126, 127, 130, 149, 157, 161, 171, 174, 177, 187, 195, 225, 302, 316, 359, 360, 363, 396, 479, 486, 491]"
我想把這個字串變成一個實際的列表.我想可以正則表達式輸出數字然后循環(append()
)但是有沒有更簡單的方法?不知道如何將其設置為正則表達式.
I would like to turn that litteral string into an actual list. I suppose to could regex out the numbers and loop over then (append()
) but is there an easier way? Not sure how I would set that up as a regex.
推薦答案
使用 ast.literal_eval.
>>> import ast
>>> i = ast.literal_eval('[22, 33, 36, 41, 46, 49, 56]')
>>> i[3]
41
這篇關于將表示列表的字符串轉換為實際的列表對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!