Divide a list into multiple lists based on a bin size

前端 未结 6 537
悲哀的现实
悲哀的现实 2021-01-18 03:02

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

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 03:13

    This will create a dict where each value is a list of elements that fit in a bin.

    import collections
    bins = collections.defaultdict(list)
    binId = lambda x: int(x*10)
    for val in vals:
        bins[binId(val)].append(val)
    

提交回复
热议问题