这实际上是两个问题。
我有一份年龄间隔列表。对于每个间隔,存在相应的值。区间和值按元组age_value_intervals
列表组织(请参阅代码中的注释)。
我还有一个单独的不同年龄列表ages
,我想知道它的价值。
以下代码是尝试将值映射到给定年龄。
现在回答问题,
为了为value_map
分配值,我使用ages
对value_map
和zip
进行迭代。然后我尝试分配给value
。这不起作用。为什么呢?
我怀疑我使用的方法是非常有效的(如果它有效)。有没有更好的方法来实现这种映射?
import numpy as np
# List of tuples defining and age interval and the corresponing value for
# that interval. For instance (20, 30, 10) indicates that the age interval from
# 20 to 30 has the value 10
age_value_intervals = [(20, 30, 10),
(30, 35, 5),
(35, 42, 50),
(50, 56, 40),
(56, 60, 30)]
# The ages for which I would like to know the value
ages = [25, 30, 35, 40, 45, 50]
# Empty array used to stor the values for the corresponding age
value_map = np.empty(len(ages))
# I want the value to be nan if there is no known value
value_map[:] = np.nan
# Iterate over the ages I want to know the value for
for age, value in zip(ages, value_map):
# Check if the age is in an interval for which the value is known
for from_age, to_age, actual_value in age_value_intervals:
if age >= from_age and age < to_age:
# Assign the value to the value_map
# This is were it falls apart (I guess...)
value = actual_value
# Move on to the next age since we got a match
break
#Expected output
value_map = [10, 5, 50, 50, nan, 40]
答案 0 :(得分:3)
我建议您将numpy.digitize
与dict
一起使用。当值无法映射到范围时,您可以手动考虑实例。
import numpy as np
age_value_intervals = [(20, 30, 10),
(30, 35, 5),
(35, 42, 50),
(50, 56, 40),
(56, 60, 30)]
ages = np.array([25, 30, 35, 40, 45, 50])
bins = np.array([x[0] for x in age_value_intervals])
mapper = dict(enumerate([x[2] for x in age_value_intervals], 1))
res = np.array([mapper[x] for x in np.digitize(ages, bins)], dtype=float)
for idx in range(len(ages)):
if not any(i <= ages[idx] <= j for i, j, k in age_value_intervals):
res[idx] = np.nan
结果:
array([ 10., 5., 50., 50., nan, 40.])
答案 1 :(得分:2)
首先,如评论中所述,如果您尝试分配给变量,您当前正在更改内部循环,那么该值就会丢失。
其次,大多数映射都是多余的。
这样的事情可能仍有待改进但应该有效:
result=[]
for check_age in ages:
for from_age, to_age, value in age_value_intervals:
if check_age in range(from_age, to_age):
result+=[value]
print result
注意,如果您需要在间隔中不时添加一些结果,则需要额外的代码。