Am trying to create dynamically generated lists depending on the configured value and needs a specific key for each list for example
configured value is 4 and list of numbers are [1,2,3,4,5,6,7,8,9] then this should be converted to
key list
-----------------
4 [1,2,3,4]
8 [5,6,7,8]
12 [9]
If configured value is 4 then always 1234 should be in first list and 5678 in second list If the values are [1,2,3,6,7,8,9] then the result should be
key list
-----------------
4 [1,2,3]
8 [6,7,8]
12 [9]
Tried below code and am able to create list but not the key. Also feels coded many lines to create the list.
def dicttest(data):
data.sort()
result = pd.DataFrame({'ids': data})
howmany = 4
listcount = (result['ids'].max() // howmany) + 1
lists = []
for x in range(listcount):
lists.append(x * howmany)
result['range'] = pd.cut(result.ids, lists, precision=0).astype(str)
result = result.groupby('range').ids.apply(list).reset_index()
return result
getting below result
range ids
---------------
(0,4] [1,2,3]
(4,8] [6,7,8]
nan [9]
It will be ok to get the result in dataframe OR dictionary with key and list of ids.