Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created January 17, 2017 05:26
Show Gist options
  • Save KentaYamada/238875717108dbc7a3e5bc51afc239a4 to your computer and use it in GitHub Desktop.
Save KentaYamada/238875717108dbc7a3e5bc51afc239a4 to your computer and use it in GitHub Desktop.
1次元配列から2次元配列へ変換する関数
def to_2dimentional_list(arr, row_no, col_no):
res = []
offset = 0
for row in range(0, row_no):
res.append(arr[offset:col_no+offset])
offset = offset + col_no
return res
a = [1,2,3,4,5,6]
b = to_2dimentional_list(a, 2, 3)
print(b) # [[1, 2, 3], [4, 5, 6]]
c = to_2dimentional_list(a, 3, 2)
print(c) # [[1, 2], [3, 4], [5, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment