-
-
Save ntung/de04fdb22173a03ba790fa16f3a1cff8 to your computer and use it in GitHub Desktop.
Revisions
-
nz-fm created this gist
Dec 18, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ """ Allows to split a dictionary into chunks of equal size. If the length of the dictionary is not divisible by the chunk size, the last chunk will be smaller. For example, if the length of the dictionary is 10 and the chunk size is 4, then we'll have three chunks: two of size 4 and one of size 2. The first function returns a list with the dictionaries and the second one creates a generator. """ def split_dictionary(input_dict, chunk_size): res = [] new_dict = {} for k, v in input_dict.items(): if len(new_dict) < chunk_size: new_dict[k] = v else: res.append(new_dict) new_dict = {k: v} res.append(new_dict) return res def gen_split_dictionary(input_dict, chunk_size): new_dict = {} for k, v in input_dict.items(): if len(new_dict) < chunk_size: new_dict[k] = v else: yield new_dict new_dict = {k: v} yield