从数组中获取值

时间:2014-01-06 08:15:10

标签: python

我尝试了很多方法让我从signalxIP获取值,但它似乎不起作用..来自appliedFieldIP和signalxIP的值不均匀

appliedFieldIP = [10,5,0,-5,-10,-5,0,5,10]
signalxIP = [2,1,0,-1,-2,-1,0,1,2]

我想要的范围是从0到-5到-10。我使用下面的代码来实现它。但我无法从signalxIP获得价值。

aIP = appliedFieldIP[:appliedFieldIP.index(min(appliedFieldIP)) + 1] # 10,5,0,-5,-10
x2 = (filter(lambda x: x <= 0 <= max(aIP), aIP)) # 0,-5,-10
print aIP >>> 10,5,0,-5,-10
print x2 >>> 0,-5,-10

例如

if x2 is 0, y2 that is from signalxIP must be 0.
   x2 is -5, y2 is -1
   x2 is -10, y2 is -2

1 个答案:

答案 0 :(得分:0)

这真的很难看,但是:

appliedFieldIP = [10,5,0,-5,-10,-5,0,5,10]
signalxIP = [2,1,0,-1,-2,-1,0,1,2]

zipped = list(zip(appliedFieldIP, signalxIP))
cutoff_index = appliedFieldIP.index(min(appliedFieldIP))+1
zipped2 = zipped[:cutoff_index]
# this aIP should be the same as aIP in your code
aIP = [pair[0] for pair in zipped2]
intermediate = list(filter(lambda x: x[0] <= 0 <= max(aIP), zipped2))
x2 = [pair[0] for pair in intermediate]
y2 = [pair[1] for pair in intermediate]

print(aIP)
print(x2)
print(y2)

结果:

[10, 5, 0, -5, -10]
[0, -5, -10]
[0, -1, -2]

基本上,我们的想法是zip appliedFieldIPsignalxIP在一起(给你zipped),然后对压缩列表执行操作,这样每当你从appliedFieldIP部分切出元素,同时从signalxIP部分切出相同的元素。最后,我们只提取signalxIP部分,向我们提供列表y2。你可以稍微缩短一点,但是我有点冗长,因为我不确定你可能需要在以后重复使用这部分(例如aIP)。