我有一個名為lists的串列。
lists = [['Was the indus valley part of the maharashtra empire?',
'Is there such a thing as golden age of indian culture?',
'Is the vedic period the same as the golden age?']]
我想將所有這些句子轉換成不同的串列,如下所示。
new_list = [['Was the indus valley part of the maharashtra empire?'],
['Is there such a thing as golden age of indian culture?'],
['Is the vedic period the same as the golden age?']]
如何將一維串列轉換為二維串列?
uj5u.com熱心網友回復:
使用串列推導,您需要遍歷串列的每個子串列,并[sentence]在遍歷子串列時對每個句子執行操作。
result = [[sentence] for sublist in lists for sentence in sublist]
uj5u.com熱心網友回復:
Numpy 還有一個reshape:
np.array(lists).reshape((-1, 1))
[['Was the indus valley part of the maharashtra empire?']
['Is there such a thing as golden age of indian culture?']
['Is the vedic period the same as the golden age?']]
uj5u.com熱心網友回復:
如果您按照@sahasrara62回答的代碼進行操作。它會給你串列中每個字符的串列。
# output for @sahasrara62 code
[['W'], ['a'], ['s'], [' '], ['t'], ['h'], ['e'], [' '], ['i'], ['n'], ['d'], ['u'], ['s'], [' '], ['v'], ['a'], ['l'], ['l'], ['e'], ['y'], [' '], ['p'], ['a'], ['r'], ['t'], [' '], ['o'], ['f'], [' '], ['t'], ['h'], ['e'], [' '], ['m'], ['a'], ['h'], ['a'], ['r'], ['a'], ['s'], ['h'], ['t'], ['r'], ['a'], [' '], ['e'], ['m'], ['p'], ['i'], ['r'], ['e'], ['?'], ['I'], ['s'], [' '], ['t'], ['h'], ['e'], ['r'], ['e'], [' '], ['s'], ['u'], ['c'], ['h'], [' '], ['a'], [' '], ['t'], ['h'], ['i'], ['n'], ['g'], [' '], ['a'], ['s'], [' '], ['g'], ['o'], ['l'], ['d'], ['e'], ['n'], [' '], ['a'], ['g'], ['e'], [' '], ['o'], ['f'], [' '], ['i'], ['n'], ['d'], ['i'], ['a'], ['n'], [' '], ['c'], ['u'], ['l'], ['t'], ['u'], ['r'], ['e'], ['?'], ['I'], ['s'], [' '], ['t'], ['h'], ['e'], [' '], ['v'], ['e'], ['d'], ['i'], ['c'], [' '], ['p'], ['e'], ['r'], ['i'], ['o'], ['d'], [' '], ['t'], ['h'], ['e'], [' '], ['s'], ['a'], ['m'], ['e'], [' '], ['a'], ['s'], [' '], ['t'], ['h'], ['e'], [' '], ['g'], ['o'], ['l'], ['d'], ['e'], ['n'], [' '], ['a'], ['g'], ['e'], ['?']]
您可以簡單地為此使用串列推導。
result = [[sentence] for sentence in lists]
# output
[['Was the indus valley part of the maharashtra empire?'], ['Is there such a thing as golden age of indian culture?'], ['Is the vedic period the same as the golden age?']]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/490270.html
下一篇:在Python中只取矩陣的一部分
