Last active
February 3, 2017 12:13
-
-
Save devlights/81b8420e4d3dedd77f4a04b98a410ffd to your computer and use it in GitHub Desktop.
[python] chunk 処理
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
| # coding: utf-8 | |
| from typing import Iterable, Iterator, Any | |
| def chunks(sequence: Iterable, chunk_size: int = 1) -> Iterator[Any]: | |
| """ | |
| 指定されたシーケンスを指定されたチャンクに分割します. | |
| :param sequence: シーケンス | |
| :param chunk_size: チャンクサイズ | |
| :return: Iterator[Any] | |
| """ | |
| for i in range(0, len(sequence), chunk_size): | |
| yield sequence[i:i + chunk_size] | |
| if __name__ == '__main__': | |
| bits = list(range(100)) | |
| results = list(chunks(list(chunks(bits, 2)), 16)) | |
| pprint.pprint(results) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results