obsFirstClassArray = [479, 1448.147, 541.692, 1184.73, 2668.818, 1159.531, 525.242, 715.489, 711.0, 422.358, 1681.012, 2418.123, 1798.0, 1756.264, 2015.973, 1318.292, 2106.042, 528.5, 415.244, 3796.11, 1047.01, 1516.165, 646.705, 1637.236, 2337.71, 1059.644, 3215.303, 2756.235, 3192.125, 3274.501, 1204.304, 1737.25, 907.489, 1824.506, 628.075, 453.591, 964.125, 710.029, 717.817, 1393.557, 1426.171, 416.072, 739.597, 419.729, 1477.92, 1046.108, 1152.492, 3415.375, 3154.497, 3641.262, 3722.41, 3611.676, 3762.858, 689.71, 3649.2, 2052.045, 1127.46, 957.21, 570.256, 673.569, 2595.0, 3622.948, 3672.778, 747.099, 2459.65, 517.867, 540.28, 514.078, 2737.375, 3221.398, 2872.977, 754.37, 1106.124, 997.063, 1224.915, 466.911, 2729.25, 3109.625, 2067.125, 1130.5, 1621.376, 1957.471, 3808.968, 2326.75, 1421.764, 7009.088, 5930.857, 9186.095, 13291.356, 23698.799, 8798.074, 21714.114, 15574.125, 11112.625, 14937.0, 4351.402, 11384.579, 10564.059, 7223.75, 27821.241, 6405.0, 5030.387, 11452.005, 4915.75, 20737.125, 16062.25, 29792.424, 7617.884, 33135.874, 15092.375, 22489.929, 22787.852, 10443.0, 20199.447, 29725.186]
def withinBoundary(p,bound):
if (bound[0] <= p < bound[1]):
return p
else:
return None
minBoundary = [403.341,33882.0162,131936.8323]
maxBoundary = [33882.0162,131936.8323,980586.2055]
bound=zip(minBoundary,maxBoundary)
for i in obsFirstClassArray:
for j in bound:
if (withinBoundary(i,j)!=None):
print i, " is between", j[0]," and ",j[1]
我想在列表中获取指定范围之间的单个数据集,例如[403.341-33882.0162]?我怎样才能做到这一点?所以,我得到的输出类似于以下
8777.9956 is between 403.341 and 33882.0162
5877.80875 is between 403.341 and 33882.0162
5673.10075 is between 403.341 and 33882.0162
76450.3483 is between 33882.0162 and 131936.8323
- 在上面的示例中输出这些数字
(8777.9956,5877.80875,5673.10075) lies between 403.341 and 33882.0162 and (76450.3483) lies between 33882.0162 and 131936.8323.
如何将输出输入到单个数字列表中?我的输出应该是
list403.341 _33882.0162 = [8777.9956,5877.80875,5673.10075]
list33882.0162_131936.8323 = [76450.3483]
我可以将list403.341 _33882.0162 = [8777.9956,5877.80875,5673.10075]存入变量吗? 我们将非常感谢您的帮助。谢谢。
答案 0 :(得分:0)
这将为您提供一个列表列表,其中每个列表都包含与相应边界相符的元素。
out = [[el for el in obsFirstClassArray if withinBoundary(el, z)]
for z in zip(minBoundary, maxBoundary)]
产生
[[479, 1448.147, 541.692, 1184.73, 2668.818, 1159.531, 525.242, 715.489, 711.0, 422.358, 1681.012, 2418.123, 1798.0, 1756.264, 2015.973, 1318.292, 2106.042, 528.5, 415.244, 3796.11, 1047.01, 1516.165, 646.705, 1637.236, 2337.71, 1059.644, 3215.303, 2756.235, 3192.125, 3274.501, 1204.304, 1737.25, 907.489, 1824.506, 628.075, 453.591, 964.125, 710.029, 717.817, 1393.557, 1426.171, 416.072, 739.597, 419.729, 1477.92, 1046.108, 1152.492, 3415.375, 3154.497, 3641.262, 3722.41, 3611.676, 3762.858, 689.71, 3649.2, 2052.045, 1127.46, 957.21, 570.256, 673.569, 2595.0, 3622.948, 3672.778, 747.099, 2459.65, 517.867, 540.28, 514.078, 2737.375, 3221.398, 2872.977, 754.37, 1106.124, 997.063, 1224.915, 466.911, 2729.25, 3109.625, 2067.125, 1130.5, 1621.376, 1957.471, 3808.968, 2326.75, 1421.764, 7009.088, 5930.857, 9186.095, 13291.356, 23698.799, 8798.074, 21714.114, 15574.125, 11112.625, 14937.0, 4351.402, 11384.579, 10564.059, 7223.75, 27821.241, 6405.0, 5030.387, 11452.005, 4915.75, 20737.125, 16062.25, 29792.424, 7617.884, 33135.874, 15092.375, 22489.929, 22787.852, 10443.0, 20199.447, 29725.186],
[],
[]]
你可以看到它包含三个列表,每个给定的一个列表。
否则,如果我误解了你要找的东西,这会给你一个元组列表,每个元组都包含被检查的元素和它所属的范围(绑定)。
该算法假设范围(边界)是相邻的和不相交的(因为它们在您提供的示例代码中),因此它可以停止并仅返回第一个匹配
from itertools import islice, dropwhile
def withinBoundary(p,bound):
return p >= bound[0] and p < bound[1]
minBoundary = [403.341,33882.0162,131936.8323]
maxBoundary = [33882.0162,131936.8323,980586.2055]
out = [(el, z) for el in obsFirstClassArray
for z in islice(dropwhile(lambda z: not withinBoundary(el, z), zip(minBoundary, maxBoundary)),1)]
print(out)
产生
[(479, (403.341, 33882.0162)),
(1448.147, (403.341, 33882.0162)),
(541.692, (403.341, 33882.0162)),
(1184.73, (403.341, 33882.0162)),
(2668.818, (403.341, 33882.0162)),
(1159.531, (403.341, 33882.0162)),
(525.242, (403.341, 33882.0162)),
(715.489, (403.341, 33882.0162)),
(711.0, (403.341, 33882.0162)),
(422.358, (403.341, 33882.0162)),
(1681.012, (403.341, 33882.0162)),
(2418.123, (403.341, 33882.0162)),
(1798.0, (403.341, 33882.0162)),
(1756.264, (403.341, 33882.0162)),
(2015.973, (403.341, 33882.0162)),
...
答案 1 :(得分:0)
使用python&#39; s filter:
for mi, ma in zip(minBoundary, maxBoundary):
print (filter(lambda x:(x > mi and x < ma),obsFirstClassArray))
您还可以使用列表理解:
for mi, ma in zip(minBoundary, maxBoundary):
print ([e for e in obsFirstClassArray if (e > mi and e < ma)])
为了存储结果,建议不要将结果绑定到变量而不是使用列表。
答案 2 :(得分:0)
def withinBoundary(p,b):
if (b[0] <= p < b[1]):
return p
else:
return None
minBoundary = [403.341,33882.0162,131936.8323]
maxBoundary = [33882.0162,131936.8323,980586.2055]
bound=zip(minBoundary,maxBoundary)
for j in bound:
list = []
for i in obsFirstClassArray:
if (withinBoundary(i,j)!=None):
list.append(i)
print "list%s - %s = "% (j[0], j[1]), list
产地:
list403.341 - 33882.0162 = [479, 1448.147, 541.692, 1184.73, 2668.818, 1159.531, 525.242, 715.489, 711.0, 422.358, 1681.012, 2418.123, 1798.0, 1756.264, 2015.973, 1318.292, 2106.042, 528.5, 415.244, 3796.11, 1047.01, 1516.165, 646.705, 1637.236, 2337.71, 1059.644, 3215.303, 2756.235, 3192.125, 3274.501, 1204.304, 1737.25, 907.489, 1824.506, 628.075, 453.591, 964.125, 710.029, 717.817, 1393.557, 1426.171, 416.072, 739.597, 419.729, 1477.92, 1046.108, 1152.492, 3415.375, 3154.497, 3641.262, 3722.41, 3611.676, 3762.858, 689.71, 3649.2, 2052.045, 1127.46, 957.21, 570.256, 673.569, 2595.0, 3622.948, 3672.778, 747.099, 2459.65, 517.867, 540.28, 514.078, 2737.375, 3221.398, 2872.977, 754.37, 1106.124, 997.063, 1224.915, 466.911, 2729.25, 3109.625, 2067.125, 1130.5, 1621.376, 1957.471, 3808.968, 2326.75, 1421.764, 7009.088, 5930.857, 9186.095, 13291.356, 23698.799, 8798.074, 21714.114, 15574.125, 11112.625, 14937.0, 4351.402, 11384.579, 10564.059, 7223.75, 27821.241, 6405.0, 5030.387, 11452.005, 4915.75, 20737.125, 16062.25, 29792.424, 7617.884, 33135.874, 15092.375, 22489.929, 22787.852, 10443.0, 20199.447, 29725.186]
list33882.0162 - 131936.8323 = []
list131936.8323 - 980586.2055 = []
~ mgregory$
请注意,您的数据不包含您在输出中所说的预期值。这是一个讨厌的伎俩。
答案 3 :(得分:0)
assgn = 'list'
for j in bound:
k = []
for i in obsFirstClassArray:
if i and j is not None:
a = j[0]
b = j[1]
lname = assgn+str(a)+' '+'_'+str(b)
if (withinBoundary(i,j)!=None):
k.append(i)
print lname,k
这是你的输出
list403.341 _33882.0162 [479, 1448.147, 541.692, 1184.73, 2668.818, 1159.531, 525.242, 715.489, 711.0, 422.358, 1681.012, 2418.123, 1798.0, 1756.264, 2015.973, 1318.292, 2106.042, 528.5, 415.244, 3796.11, 1047.01, 1516.165, 646.705, 1637.236, 2337.71, 1059.644, 3215.303, 2756.235, 3192.125, 3274.501, 1204.304, 1737.25, 907.489, 1824.506, 628.075, 453.591, 964.125, 710.029, 717.817, 1393.557, 1426.171, 416.072, 739.597, 419.729, 1477.92, 1046.108, 1152.492, 3415.375, 3154.497, 3641.262, 3722.41, 3611.676, 3762.858, 689.71, 3649.2, 2052.045, 1127.46, 957.21, 570.256, 673.569, 2595.0, 3622.948, 3672.778, 747.099, 2459.65, 517.867, 540.28, 514.078, 2737.375, 3221.398, 2872.977, 754.37, 1106.124, 997.063, 1224.915, 466.911, 2729.25, 3109.625, 2067.125, 1130.5, 1621.376, 1957.471, 3808.968, 2326.75, 1421.764, 7009.088, 5930.857, 9186.095, 13291.356, 23698.799, 8798.074, 21714.114, 15574.125, 11112.625, 14937.0, 4351.402, 11384.579, 10564.059, 7223.75, 27821.241, 6405.0, 5030.387, 11452.005, 4915.75, 20737.125, 16062.25, 29792.424, 7617.884, 33135.874, 15092.375, 22489.929, 22787.852, 10443.0, 20199.447, 29725.186]
list33882.0162 _131936.8323 []
list131936.8323 _980586.2055 []