I have a list containing more than 100,000 values in it.
I need to divide the list into multiple smaller lists based on a specific bin width say 0.1. Can anyone hel
We can make bins with more_itertools, a third-party library.
Given
iterable = (
"-0.234 -0.04325 -0.43134 -0.315 -0.6322 -0.245 "
"-0.5325 -0.6341 -0.5214 -0.531 -0.124 -0.0252"
).split()
iterable
# ['-0.234', '-0.04325', '-0.43134', '-0.315', '-0.6322', '-0.245', '-0.5325', '-0.6341', '-0.5214', '-0.531', '-0.124', '-0.0252']
Code
import more_itertools as mit
keyfunc = lambda x: float("{:.1f}".format(float(x)))
bins = mit.bucket(iterable, key=keyfunc)
keys = [-0.0,-0.1,-0.2, -0.3,-0.4,-0.5,-0.6]
a,b,c,d,e,f,g = [list(bins[k]) for k in keys]
c
# ['-0.234', '-0.245']
Details
We can bin by the key function, which we define to format numbers to a single precision, i.e. -0.213
to -0.2
.
keyfunc = lambda x: float("{:.1f}".format(float(x)))
bins = mit.bucket(iterable, key=keyfunc)
These bins are accessed by the keys defined by the key function:
c = list(bins[-0.2])
c
# ['-0.234', '-0.245']
Access all bins by iterating keys:
f = lambda x: float("{:.1f}".format(float(x)))
bins = mit.bucket(iterable, key=keyfunc)
keys = [-0.0,-0.1,-0.2, -0.3,-0.4,-0.5,-0.6]
for k in keys:
print("{} --> {}".format(k, list(bins[k])))
Output
-0.0 --> ['-0.04325', '-0.0252']
-0.1 --> ['-0.124']
-0.2 --> ['-0.234', '-0.245']
-0.3 --> ['-0.315']
-0.4 --> ['-0.43134']
-0.5 --> ['-0.5325', '-0.5214', '-0.531']
-0.6 --> ['-0.6322', '-0.6341']
List comprehension and unpacking is another option (see Code example).
See also more_itertools docs for more details.