我制作了二維陣列
rows, columns = (5, 4)
table = [["" for i in range(columns)] for j in range(rows)]
現在我想將每個字串字符插入其中
string = "aaa bb cccc d eee"
我想要這個輸出: [['a', 'a', 'a', ''], ['b', 'b', '', 'c'], ['c', 'c', ' c', ''], ['d', '', 'e', 'e'], ['e', '', '', '']]
我在很多方面都嘗試過這樣的事情,但它會引發錯誤。
for i in range(len(string)):
table[columns][rows] = string[i]
uj5u.com熱心網友回復:
rows, columns = (5, 4)
table = [["" for i in range(columns)]for j in range(rows)]
string = "aaa bb cccc d eee"
for i in range(len(string)):
table[i // columns][i % columns] = string[i]
print(table)
uj5u.com熱心網友回復:
用你的桌子和繩子
rows, columns = (5, 4)
table = [['' for i in range(columns)] for j in range(rows)]
string = "aaa bb cccc d eee"
您可以在回圈table行時將table的元素設定為字串字母。
str_iter = iter(string)
for row in table:
for e, (_, row[e]) in enumerate(zip(row, str_iter)): ...
print(table)
輸出
[['a', 'a', 'a', ' '],
['b', 'b', ' ', 'c'],
['c', 'c', 'c', ' '],
['d', ' ', 'e', 'e'],
['e', '', '', '']]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424312.html
上一篇:Envoy訪問日志格式驗證
