我有一個這樣的清單。
z=[']\'What type of humans arrived on the Indian subcontinent from Africa?\', \'When did humans first arrive on the Indian subcontinent?\', \'What subcontinent did humans first arrive on?\', \'Between 73000 and what year ago did humans first arrive on the Indian subcontinent?\',\kingdoms were established in Southeast Asia?Indianized\']']
我想把它轉換成簡單的二維串列。
z= [['What type of humans arrived on the Indian subcontinent from Africa?', 'When did humans first arrive on the Indian subcontinent?', 'What subcontinent did humans first arrive on?', 'Between 73000 and what year ago did humans first arrive on the Indian subcontinent?','kingdoms were established in Southeast Asia?Indianized']]
那么如何將此串列轉換為二維串列?
uj5u.com熱心網友回復:
邏輯并不完全清楚。我會在 2 個或更多非單詞字符上使用正則運算式來拆分它:
[[x for x in re.split(r'[^a-z0-9\?]{2,}', s, flags=re.I) if x] for s in z]
輸出:
[['What type of humans arrived on the Indian subcontinent from Africa?',
'When did humans first arrive on the Indian subcontinent?',
'What subcontinent did humans first arrive on?',
'Between 73000 and what year ago did humans first arrive on the Indian subcontinent?',
'kingdoms were established in Southeast Asia?Indianized']]
uj5u.com熱心網友回復:
您可以使用庫重新。它將替換特殊字符的所有正則運算式。末尾有空格(在 9 之后),它將保留空格。如果您不想要空格,請將其洗掉。
import re
re.sub('[^A-Za-z0-9 ] ', '', mystring)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490559.html
上一篇:如何從csv檔案中獲取值并遍歷列
下一篇:寫入文本檔案時如何水平列印?
