简体   繁体   English

根据百分比分配元素

[英]Distribute elements based on percentages

Let's say that I want to distribute a number of items (n) into an array of fixed size (x). 假设我想将多个项目(n)分配到固定大小(x)的数组中。 The difficult part is that I have to distribute the items using a flexibility array. 困难的部分是我必须使用灵活性数组来分发项目。

Assuming that x = 4 , n = 11 and flexibility = [20, 20, 30, 30] with len(flexibility) == x . 假设x = 4n = 11flexibility = [20, 20, 30, 30] len(flexibility) == x flexibility = [20, 20, 30, 30]len(flexibility) == x

My question is: How can I distribute the n elements in an array of length equal to x using the percentage defined in f? 我的问题是:如何使用f中定义的百分比将n个元素分布在长度等于x的数组中?

What I want at the end is something like: 我最后想要的是这样的:

n = 11
x = 4
flexibility = [20, 20, 30, 30]
distributed = distribute_elements_in_slots(n, x, flexibility)
print(distributed)
# distributed = [2, 2, 3, 4]

In the case of equal flexibility values, the final result will depend on the rule that we decide to apply to use all the item. 在弹性值相等的情况下,最终结果将取决于我们决定应用所有项目的规则。 In the previous case, the final result will be good with [2, 2, 3, 4] and with [2, 2, 4, 3]. 在前一种情况下,使用[2,2,3,4]和[2,2,4,3]最终结果会很好。

Edit : An example of the method that I want to have is as follows: 编辑 :我想要的方法的示例如下:

def distribute_elements_in_slots(n, x, flexibility=[25,25,25,25]):
    element_in_slots = []

    element_per_percentage = x / 100

    for i in range(x):
        element_in_slots.append(round(slots_per_point_percentage * flexibility[i])

Edit 2 : One of the solutions that I found is the following: 编辑2 :我发现的解决方案之一是:

def distribute_elements_in_slots(n, x, flexibility=[25,25,25,25]):
    element_in_slots = [f * n / 100 for f in flexibility]

    carry = 0
    for i in range(len(element_in_slots)):
        element = element_in_slots[i] + carry
        element_in_slot[i] = floor(element)
        carry = element- floor(element)

    if np.sum(element_in_slots) < n:
        # Here the carry is almost 1
        max_index = element_in_slots.index(max(flexibiliyt))
        appointments_per_slot[max_index] = appointments_per_slot[max_index] + 1

This will distribute almost evenly the slots based on the flexibility array. 这将根据灵活性阵列几乎均匀地分配插槽。

what you need to do is split the number 11 according to certain percents given in the array so initially it becomes percentage * number(11) . 您需要做的是根据数组中给定的百分比对数字11进行分割,因此最初它变成percentage * number(11) Then we get remainder and put assign it somewhere which in your case is the last element. 然后我们得到余数并将其分配给您所处的最后一个元素。

In [10]: [i*n/100 for i in f]
Out[10]: [2.2, 2.2, 3.3, 3.3]

In [11]: b=[i*n/100 for i in f]

In [12]: rem = sum(b) - sum(map(int,b))


In [13]: rem
Out[13]: 1.0

In [24]: b= list(map(int,b))

In [26]: b[-1] +=rem

In [27]: b
Out[27]: [2, 2, 3, 4.0]

Hope it helps. 希望能帮助到你。 :) :)

As Albin Paul did, we need to allocate the whole-number amount for each slot's percentage. 就像阿尔宾·保罗(Albin Paul)一样,我们需要为每个老虎机百分比分配整数数量。 The leftovers need to be allocated, largest first. 剩余的部分需要分配,首先最大。

def distribute_elements_in_slots(total, slots, pct):
    # Compute proportional distribution by given percentages.
    distr = [total * pct[i] / 100 for i in range(slots)]
    # Truncate each position and store the difference in a new list.
    solid = [int(elem) for elem in distr]
    short = [distr[i] - solid[i] for i in range(slots)]
    print(distr)
    print(solid)
    print(short)

    # allocate leftovers
    leftover = int(round(sum(short)))
    print(leftover)
    # For each unallocated item,
    #   find the neediest slot, and put an extra there.
    for i in range(leftover):
        shortest = short.index(max(short))
        solid[shortest] += 1
        short[shortest] = 0
        print("Added 1 to slot", shortest)

    return solid


n = 11
x = 4
flexibility = [20, 20, 30, 30]
distributed = distribute_elements_in_slots(n, x, flexibility)
print(distributed)
# distributed = [2, 2, 3, 4]

Output: 输出:

[2.2, 2.2, 3.3, 3.3]
[2, 2, 3, 3]
[0.2, 0.2, 0.3, 0.3]
1
Added 1 to slot 2
[2, 2, 4, 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM