将整数转换为给定间隔的二进制的Python函数

huangapple go评论54阅读模式
英文:

Python function Converting an integer to bins as per given interval

问题

I am trying to convert an integer (num) to bins as per given interval.
the bin size (intervals) are [1, 200), [200, 400), [400, 800), [800, 1200), [1200, num]

I'm doing it in a crude way ...

def create_bins(num):
    """Create Bins as per given intervals."""
    s1, s2, s3, s4 = 199, 200, 400, 400
    if num > 1200:
        res = [s1, s2, s3, s4, num - (s1 + s2 + s3 + s4)]
    elif num < 1200 and num >= 800:
        res = [s1, s2, s3, num - (s1 + s2 + s3)]
    elif num < 800 and num >= 400:
        res = [s1, s2, num - (s1 + s2)]
    elif num < 400 and num >= 200:
        res = [s1, num - s1]
    else:
        res = [num]

    return res

This function for create_bins(1041) returns [199, 200, 400, 242] which is correct. However, I'm sure there are better ways to get it done...

Will appreciate if you can lead me towards better solutions for these kinds of problems.

英文:

I am trying to convert an integer (num) to bins as per given interval.
the bin size (intervals) are [1, 200), [200, 400), [400, 800), [800, 1200), [1200, num]

I'am doing it in a crude way ...

def create_bins(num):
    &quot;&quot;&quot;Create Bins as per given intervals.&quot;&quot;&quot;
    s1, s2, s3, s4 = 199, 200, 400, 400
    if num &gt; 1200:
        res = [s1, s2, s3, s4, num - (s1 + s2 + s3 + s4)]
    elif num &lt; 1200 and num &gt;= 800:
        res = [s1, s2, s3, num - (s1 + s2 + s3)]
    elif num &lt; 800 and num &gt;= 400:
        res = [s1, s2, num - (s1 + s2)]
    elif num &lt; 400 and num &gt;= 200:
        res = [s1, num - s1]
    else:
        res = [num]

    return res

this function for create_bins(1041) returns [199, 200, 400, 242] which is correct. However, I'am sure there are better ways to get it done...

Will appreciate if you can lead me towards better solutions for these kind of problem.

答案1

得分: 1

这可能是个好主意。

def create_bins_new(num):
    val = [199, 200, 400, 400]
    out = []
    for v in val:
        if v < num:
            out.append(v)
            num -= v
    out.append(num)
    return out
英文:

this may be good idea.

  def create_bins_new(num):
        val = [199,200,400,400]
        out = []
        for v in val:
            if v &lt; num:
                out.append(v)
                num -= v
        out.append(num)
        return out

答案2

得分: 0

在处理大量的箱子时,计算箱子尺寸的前缀和,通过二分查找在该列表中定位 num 并组装结果。

import bisect

# 数据数组
bins = [199, 200, 400, 400]
prefix = [sum(bins[:i]) for i in range(len(bins)+1)]

# 尝试不同的值
for num in [1, 150, 199, 200, 201, 798, 799, 800, 801, 1041, 2000]:
    # 搜索和连接
    index = bisect.bisect_left(prefix, num) - 1
    print(num, bins[:index] + [num - prefix[index]])

输出:

1 [1]
150 [150]
199 [199]
200 [199, 1]
201 [199, 2]
798 [199, 200, 399]
799 [199, 200, 400]
800 [199, 200, 400, 1]
801 [199, 200, 400, 2]
1041 [199, 200, 400, 242]
2000 [199, 200, 400, 400, 801]
英文:

In case of a very large number of bins, compute the prefix sum of the bin sizes, locate num in this list by binary search and assemble the result.

import bisect

# Data arrays
bins= [199, 200, 400, 400]
prefix= [sum(bins[:i]) for i in range(len(bins)+1)]

# Try various values
for num in [1, 150, 199, 200, 201, 798, 799, 800, 801, 1041, 2000]:
    # Search and concatenete
    index= bisect.bisect_left(prefix, num) - 1
    print(num, bins[:index] + [num - prefix[index]])

Output:

1 [1]
150 [150]
199 [199]
200 [199, 1]
201 [199, 2]
798 [199, 200, 399]
799 [199, 200, 400]
800 [199, 200, 400, 1]
801 [199, 200, 400, 2]
1041 [199, 200, 400, 242]
2000 [199, 200, 400, 400, 801]

huangapple
  • 本文由 发表于 2023年7月18日 12:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定