我感觉有一个优雅的Python解决方案,我一直在尝试使用VBA解决。 有人可以帮忙吗?
如何生成符合以下条件的数字列表:
此问题的示例:
总计20%的剩余单位是无人(U)占用单位。
所以:
Construction_type_categories=[.4, .6]
(102个单元分为工作室和联排别墅:40%和60%)Assisted=[.1,.4,.5]
(有80%以上的单位为辅助单位,根据列表中的百分比进一步分为老年人,家庭和其他人)结果:
[4,16,12,8,7,25,19,11]
故障:
我想到先生成一个四舍五入的初步数组,然后循环遍历并进行调整。 看起来很乏味,我已经开始考虑使用numpy生成大型的数字矩阵并按照概述的条件进行过滤。
手动生成和优化数字非常耗时,因此,感谢您提供更好的解决方案。
import math
def is_even(x):
if x % 2 == 0:
return True
else:
return False
total=102
unassisted=.2
unassisted_unit_count= math.floor(total*unassisted)
assisted_unit_count=total- unassisted_unit_count
construction_type_categories=[.4, .6] #Must be even.
assisted_subcategories=[.1,.4,.5] #Last element here will be of least priority.
def Unit_Number_Getter(L):
if L[1]=='total_constr_type_amounts':
giventotal= total
if L[1]=='assisted_constr_type_amounts':
giventotal= assisted_unit_count
#Spliting by construction type (preliminary).
constr_type_amounts=[]
for ct in construction_type_categories:
constr_type_amounts.append(round(giventotal * ct))
#Making the unit counts even for the total construction type amounts (need to).
for p in constr_type_amounts:
if not is_even(p):
add_here=constr_type_amounts.index(p)
for f in constr_type_amounts:
if not is_even(f):
from_here= constr_type_amounts.index(f)
constr_type_amounts[add_here] +=1
constr_type_amounts[from_here] -=1
assert sum(constr_type_amounts)==giventotal
print L[1]
print(constr_type_amounts)
L[0]=constr_type_amounts
total_constr_type_amounts=0
assisted_constr_type_amounts=0
List_of_lists=[[total_constr_type_amounts,"total_constr_type_amounts"],[assisted_constr_type_amounts,"assisted_constr_type_amounts"]]
#Established the unit counts for each construction type (for Assisted and total units)
for L in List_of_lists:
Unit_Number_Getter(L)
#Getting a list of the unit counts for each assisted subcategory in each constr type.
testlist=[]
for c in List_of_lists[1][0]:
already_added=0
for a in assisted_subcategories[:-1]:
adding_now= math.ceil(c * a)
testlist.append(adding_now)
already_added+=adding_now
#^Added the priority assisted units (all but the last element).
#Now will add the last of the assisted units.
testlist.append(c-already_added)
#Now will add he least prioritized unassisted units up to the max.
Max_unassisted_units= List_of_lists[0][0][List_of_lists[1][0].index(c)]-c
testlist.append(Max_unassisted_units)
assert ((c+Max_unassisted_units)== List_of_lists[0][0][List_of_lists[1][0].index(c)])#all units present
print("Result: "+ "\n" + str(testlist))