3 回答

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
你可以試試:
out = np.full((rows,cols), np.nan, dtype='object') out.ravel()[:len(alist)] = alist
輸出:
array([['a', 'b'], ['c', nan]], dtype=object)
作為旁注,這可能對(duì)您更好:
rows = int(np.ceil(len(alist)/cols))

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
嘗試(沒(méi)有任何外部庫(kù))
import math
alist = ['a', 'b', 'c']
cols = 2
new_list = []
steps = math.ceil(len(alist) / cols)
start = 0
for x in range(0, steps):
new_list.append(alist[x * cols: (x + 1) * cols])
new_list[-1].extend([None for t in range(cols - len(new_list[-1]))])
print(new_list)
輸出
[['a', 'b'], ['c', None]]

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以使用列表理解來(lái)實(shí)現(xiàn)結(jié)果:
li = ['a','b','c']
l = len(li)
new_list = [li[x:x+2] for x in range(l // 2)]
if l % 2 != 0:
new_list.append([li[-1], None])
print(new_list) # [['a', 'b'], ['c', None]]
添加回答
舉報(bào)