Skip to content

Instantly share code, notes, and snippets.

@utgwkk
Last active December 21, 2015 11:39
Show Gist options
  • Save utgwkk/c860b12dfb4d4f982945 to your computer and use it in GitHub Desktop.
Save utgwkk/c860b12dfb4d4f982945 to your computer and use it in GitHub Desktop.
リストをこういうふうに分割する
def split_list(xs, span=1):
result = []
arr = []
for i, x in enumerate(xs):
if i > 0 and i % span == 0:
result.append(arr)
arr = []
arr.append(x)
else:
if len(arr) > 0:
result.append(arr)
return result
if __name__ == '__main__':
input_list = [
([1,2,3,4,5,6], 2),
([1,1,4,5,1,4], 5),
([1,1,1,1], 1)
]
output_list = [
[[1,2],[3,4],[5,6]],
[[1,1,4,5,1],[4]],
[[1],[1],[1],[1]],
]
for inp, outp in zip(input_list, output_list):
assert split_list(*inp) == outp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment