Last active
December 21, 2015 11:39
-
-
Save utgwkk/c860b12dfb4d4f982945 to your computer and use it in GitHub Desktop.
リストをこういうふうに分割する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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