Reputation: 2567
I am trying to split a big list into a dictionary. Given a list with say 363
elements I want to divide them such that each key in the dictionary will have a list of 10 values. So a list of 363 elements will have 37 keys (rounded up to nearest 10).The keys will start from 1
and go upto 37
. I wrote a function for the rounding up but i dont know how to split the list while making sure the sequence is maintained and if all the lists (which will be the values of the keys) are joined they will mirror the original list.
This is my rounding up function:
def roundup(list_length, atatime=10):
return ((atatime-(list_length%atatime))+list_length)/atatime
Any ideas , suggestions will be really helpfull
Upvotes: 1
Views: 1644
Reputation: 180550
If you want to round up and slice appropriately:
l = list(range(363))
at_a_time = 10
n = len(l)
d, r = divmod(n, at_a_time)
# if there is a remainder, add 1 to the keys, 363 -> 37 keys 360 -> 36 keys
num_keys = d + 1 if r else d
# get correct slice size based on amount of keys
sli = n // num_keys
# create "sli" sized chunks
vals = (l[i:i+sli] for i in range(0, n, sli+1))
# make dict from `1 to num_keys inclusive and slices
dct = dict(zip(range(1,num_keys+1),vals))
print(dct)
{1: [0, 1, 2, 3, 4, 5, 6, 7, 8], 2: [10, 11, 12, 13, 14, 15, 16, 17, 18], 3: [20, 21, 22, 23, 24, 25, 26, 27, 28], 4: [30, 31, 32, 33, 34, 35, 36, 37, 38], 5: [40, 41, 42, 43, 44, 45, 46, 47, 48], 6: [50, 51, 52, 53, 54, 55, 56, 57, 58], 7: [60, 61, 62, 63, 64, 65, 66, 67, 68], 8: [70, 71, 72, 73, 74, 75, 76, 77, 78], 9: [80, 81, 82, 83, 84, 85, 86, 87, 88], 10: [90, 91, 92, 93, 94, 95, 96, 97, 98], 11: [100, 101, 102, 103, 104, 105, 106, 107, 108], 12: [110, 111, 112, 113, 114, 115, 116, 117, 118], 13: [120, 121, 122, 123, 124, 125, 126, 127, 128], 14: [130, 131, 132, 133, 134, 135, 136, 137, 138], 15: [140, 141, 142, 143, 144, 145, 146, 147, 148], 16: [150, 151, 152, 153, 154, 155, 156, 157, 158], 17: [160, 161, 162, 163, 164, 165, 166, 167, 168], 18: [170, 171, 172, 173, 174, 175, 176, 177, 178], 19: [180, 181, 182, 183, 184, 185, 186, 187, 188], 20: [190, 191, 192, 193, 194, 195, 196, 197, 198], 21: [200, 201, 202, 203, 204, 205, 206, 207, 208], 22: [210, 211, 212, 213, 214, 215, 216, 217, 218], 23: [220, 221, 222, 223, 224, 225, 226, 227, 228], 24: [230, 231, 232, 233, 234, 235, 236, 237, 238], 25: [240, 241, 242, 243, 244, 245, 246, 247, 248], 26: [250, 251, 252, 253, 254, 255, 256, 257, 258], 27: [260, 261, 262, 263, 264, 265, 266, 267, 268], 28: [270, 271, 272, 273, 274, 275, 276, 277, 278], 29: [280, 281, 282, 283, 284, 285, 286, 287, 288], 30: [290, 291, 292, 293, 294, 295, 296, 297, 298], 31: [300, 301, 302, 303, 304, 305, 306, 307, 308], 32: [310, 311, 312, 313, 314, 315, 316, 317, 318], 33: [320, 321, 322, 323, 324, 325, 326, 327, 328], 34: [330, 331, 332, 333, 334, 335, 336, 337, 338], 35: [340, 341, 342, 343, 344, 345, 346, 347, 348], 36: [350, 351, 352, 353, 354, 355, 356, 357, 358], 37: [360, 361, 362]}
If you don't want the list sliced evenly and the remainder to be added to the last key just use your atatime
for each slice (10 in the example code):
vals = (l[i:i+at_a_time] for i in range(0, n, sli+1))
We can also use num_keys = n + (at_a_time - 1) // at_a_time
to round as any value that has a remainder // at_a_time
will be rounded up adding at_a_time - 1
to it , any number evenly divisible will not.
You can do it all in a dict comprehension making some changes to the code but I think you will hopefully learn more from explicit code.
Upvotes: 1
Reputation: 850
The following dict comprehension slices the list into the required number of slices.
In other words, idx+1
is the key, whereas lst[atatime*idx:atatime*(idx+1)]
is the value of a dictionary defined using dict-comprehension. The value is a slice of atatime
length. We iterate over the (rounded upwards) size of the list divided by the chunk-size using an xrange iterator.
from math import ceil
{idx+1: lst[atatime*idx:atatime*(idx+1)] for idx in xrange(int(ceil(float(len(lst))/atatime)))}
Upvotes: 0