简体   繁体   English

分配给循环值

[英]Assigning to for-loop values

This is really two questions. 这确实是两个问题。

I have a list of age intervals. 我有年龄间隔清单。 For each interval there is a corresponding value. 对于每个间隔,都有一个对应的值。 The intervals and values are organized in a list of tuples age_value_intervals (see the comments in the code). 时间间隔和值在元组age_value_intervals列表中进行组织(请参见代码中的注释)。

I also have a separate list of distinct ages, ages , for which I would like to know the value. 我也有一个单独的清单,列出了不同的年龄段, ages ,我想知道这些值。

The code below is an attempt at mapping the value to the given age. 下面的代码尝试将值映射到给定的年龄。

Now to the questions, 现在到问题,

  1. In order to assign a value to the value_map I iterate over both ages and value_map using zip . 为了分配一个值value_map我遍历两个agesvalue_map使用zip I then try to assign to value . 然后我试图给value This doesn't work. 这行不通。 Why? 为什么?

  2. I doubt that the method I use is very efficient (if it had worked). 我怀疑我使用的方法是否有效(如果可行)。 Is there a better way to achieve this mapping? 有没有更好的方法来实现此映射?


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]

I recommend you use numpy.digitize together with dict for this. 我建议您numpy.digitize使用numpy.digitizedict You can manually account for instances when a value cannot be mapped to a range. 当值无法映射到范围时,您可以手动考虑实例。

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

Result: 结果:

array([ 10.,   5.,  50.,  50.,  nan,  40.])

First, as noted on comments, if you try to assign to variable you are currently changing inside loop, the value simply gets lost. 首先,如注释中所述,如果您尝试分配给当前在循环内更改的变量,则该值会丢失。

Secondly most of the mappings are redundant. 其次,大多数映射是多余的。

Something like this can probably still be improved but should work: 这样的事情可能仍然可以改善,但应该可以:

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

Note, if you need some result added also when the age is not in the interval, there needs to be additional code. 请注意,如果您还需要在年龄不在间隔内时添加一些结果,则需要附加代码。

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

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