z= ["]['This urbanisation was accompanied by the rise of new ascetic movements in __________________ Magadha including Jainism and Buddhism.', 'Greater'], ['During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle __________________ and the Mediterranean.', 'East']]"]
type(z) #list
lists = [[z[0].strip("'[]")]]
type(lists) #list
從這段代碼中,我得到一個串列將是這樣的。
lists=[["This urbanisation was accompanied by the rise of new ascetic movements in __________________ Magadha including Jainism and Buddhism.', 'Greater'],['During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle __________________ and the Mediterranean.', 'East"]]
現在我想用單引號替換雙引號。這樣這個串列就會變成一個二維串列。
我嘗試使用下面給出的代碼。但這無濟于事。
y=str(lists).replace('"',"'")
list(y)
那么如何將串列作為 2D 串列獲取呢?
uj5u.com熱心網友回復:
import ast
lists=[["This urbanisation was accompanied by the rise of new ascetic movements in __________________ Magadha including Jainism and Buddhism.', 'Greater'],['During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle __________________ and the Mediterranean.', 'East"]]
y=str(lists).replace('"',"'")
y= ast.literal_eval(y)
#demonstration
for i in y:
print(i)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/490273.html
