【发布时间】:2020-06-06 10:57:36
【问题描述】:
我的项目需要帮助。
事情是这样的:
prod = [0, 5, 5]
dur = [5, 5, 3]
activity = [2, 3, 4]
max_r = 6
现在,我要做的是选择一个符合以下条件的活动:
max_prod = max(prod)
if max_prod <= max_r:
# if yes and appears more than once, check dur, the activity with the highest duration will be put to select_act
if prod.count(max_prod) > 1:
# know the indexes of the values that appears more than once in prod
idx = list_duplicates_of(prod, max_prod)
# then get duration of these values
dur_same = [d for a, d in zip(prod, dur) if a == max_prod]
print(dur_same)
#select the activity that has the highest duration
#get dur of that selected activity
# if yes and appears only once, get activity and put in select_act
else:
select_act = [activity[prod.index(max_prod)]]
dur_select_act = [dur[prod.index(max_prod)]]
问题是:prod 有 3 索引。如果我要执行上面的代码,max_prod 有indexes 1 and 2,而当我尝试打印它的dur_same 时,我得到的[5, 3] 与它在产品中的假定索引不匹配。如何确保/匹配dur_same 的索引与其在prod 中的索引,然后将0 用于其他索引/es?
我在该部分的预期输出应该是:
prod = [0, 5, 5]
dur_same = [0, 5, 3] #instead of just [5, 3] from the code above
这样,调用其对应的活动会更容易。因为我所要做的就是将dur_same 中的最高持续时间与dur 匹配,并在dur 中获取匹配的索引。然后,使用该索引获取活动。
编辑:
如果对于 dur 具有相同最大值的实例,假设为dur_same = [0, 5, 5]。我怎么能在两个5s之间随机选择,然后选择它对应的activity呢?
任何帮助将不胜感激!谢谢!
【问题讨论】:
标签: python python-3.x indexing insert