假设我在Python中有一个包含x值的数组
[1 2 3 4 5]
以及相应的y值数据数组
[10 11 6 2.5 0]
现在假设我想将x的域限制为
[2 3 4]
如何生成相应的y数组?
[11 6 2.5]
发布于 2020-06-09 22:24:24
假设您设置了start_index和end_index:
x_restricted = x_values[start_index:end_index]
y_restricted = y_values[start_index:end_index]注Python使用半开区间,这意味着排除了end_index中的元素本身。
发布于 2020-06-09 22:20:50
>>> x = [1, 2, 3, 4, 5]
>>> y = [10, 11, 6, 2.5, 0]
>>> x_filtered = [2, 3, 4]
>>> [ey for ex, ey in zip(x, y) if ex in x_filtered]
[11, 6, 2.5]发布于 2020-06-09 22:23:31
您可以使用list的索引方法。
但请注意,这将假设x具有唯一的整数,并且两个值都存在于x中。
y[x.index(2):x.index(5)+1]https://stackoverflow.com/questions/62284749
复制相似问题