如何在数字范围之间创建条件?

时间:2015-12-08 04:55:57

标签: python list conditional-statements

我有一组像

这样的数字
761.034 280.792 1142.035    1473.108    240.161 1667.306    225.841 84.245  180.834 914.654 365.666 463.76  201.261 530.277 1502.179    163.777 751.058 3840.142    1504.027    720.014 1544.924    338.87  2214.548    114.412 2508.236    3015.109    2227.062    2674.247    446.8   3200.381    186.501 3433.5  1360.25 504.125 1387.75 153.183 204.053 2180.5  3364.209    251.891 108.054 361.49  349.774 113.02  3076.375    3193.251    504.865 1178.666    2358.75 7104.635    4137.363    5522.5  22025.266   14775.789   5294.174    135.772 9651.427    19914.228   15368.504   11429.375   4929.83 5145.915    4516.875    4012.156    10400.619   6512.004    9485.858    4538.629    26583.048   8598.498    185.651 4025.25 5119.157    15293.04    8668.875    11757.395   4935.5  11553.065   36806.595   6470.375    4092.992    7669.664    19135.75    5257.741    20420.314   20688.019   21863.261   10241.523   9976.349    32122.282   22491.985   29681.914   17690.724   22415.69    17587.761   11973.575   5790.75 35173.349   30926.923   28498.211   17162.425   46045.996   4388.25 5175.713    9082.515    23208.554   15334.599   69.853  42911.865   14892.464   20297.875   30944.171   8060.256    35065.112   26067.375   50756.794   34873.227   8894    32465.589   25128.902   96233.597   35112.087   7146.235    21936.5 70697.298   34218.374   17536.09    43229.971   29955.875   4250.5  4625.687    35579.206   6808.75 64457.125   8824.943    9082.113    31594.5 6902.11 104983.413  6367.362    7076.174    41888.712   6761.322    39413.5 5532.505    65454.967   78195.849   11239.07    490263.824  69842.506   13100.613   6051.5  7785.255    4785.808    14750.411   27350.637   6048.25 44466.125   4889    4415.838    8942.3  14012.006   31.386  32936.059   11097.625   19790.392   7927.011    5055.02 39588.25    11087.125   5188    55987.303   6497.743    11631.851   40733.028   33154.951   5789.055    9165.509    23321.188   13117.635   19127.535   20958.713   34520.707   14100.465   86463.009   74320.789   26178.678   13053.767   11915.875   31852.149   48303.684   11721.258   36161.408   23952.118   6159    6036.56 44876.625   23137.158   11177.5 7654.334    26652.538   14604.125   17729.084   51812.403   6771.617    37903.72    48301.506   18498.043   12688.75    67394.535   35169.467   58754.865   24433.865   23013.373   45027.7 42593.75    51453.005   24524.917   49874.454   33731.941   6520.611    37651   38547.639   85710.803   44553.525   41470.619   69035.63    74483.605   33368.257   29089.386   84528.375   15140.625   100592.358  72613.075   62537.359   142.537 53588.625   589.875 543.922 317.252 132.847 1895.827    2479.461    704.028 206.5   509.292 346.75  1178.75 1558.258    111.095 3930.911    2700.068    1789.5  221.167 595.163 234.638 960.925 1109.625    813.807 628.258 143.73  1748.905    1980    2102.316    1528.348    231.023 528.016 1772.639    151.931 667.23  669.441 2042.479    1448.147    541.692 1184.73 2668.818    1159.531    525.242 715.489 198.921 191.838 711 422.358 1681.012    2418.123    121.462 1798    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 283.667 1824.506    628.075 289.162 453.591 175.199 131.534 136.713 137.924 161.768 964.125 710.029 717.817 1393.557    1426.171    416.072 739.597 419.729 253.966 223.5   1477.92 214.507 368.125 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  105.72  570.256 673.569 2595    3622.948    3672.778    747.099 372.5   2459.65 136.023 111.875 517.867 540.28  514.078 2737.375    3221.398    115.801 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    358.25  7009.088    5930.857    9186.095    13291.356   23698.799   8798.074    21714.114   53.73   34979.396   15574.125   36881.488   11112.625   14937   4351.402    11384.579   10564.059   7223.75 27821.241   6405    5030.387    11452.005   4915.75 20737.125   16062.25    69619.345   29792.424   7617.884    33135.874   15092.375   22489.929   22787.852   35338.25    10443   20199.447   35520.363   35357.176   29725.186   43693.99    40619.199   21780.502   39585.245   45681.051   24077.923   64342.311   44671.274   84332.504   28902.155   4822.499    5587.998    8574.246    750.75  8669.126    583.384 591.694 3186.982    529.514 2764.805    3904.659    497.642         

我需要编写一个在定义范围内打印值的条件,例如

403.341-33882.0162
33882.0162-131936.8323
131936.832-980586.2055

0.0-19.0
10.0-43.0
79.0-180.0
180-9999.0

31.386 - 9976.349
9976.349 - 27821.241
27821.241 - 55987.303
55987.303 -104983.413
104983.413- 490263.824

totalObs = [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]

list1 = []
list2 = []
for item in obsinArray: 
    if item >= 403.341 and item <33882.0162:
         list1.append(item)
    elif item>= 33882.0162 and item <131936.8323:
         list2.append(item)
print [list1,list2]
  

我不确定如何满足该特定范围的条件。任何帮助将不胜感激。三江源

1 个答案:

答案 0 :(得分:0)

def WithinBounds(x,Boundary):
    if (Boundary[0] <= x < Boundary[1]):
        return x
    else:
        return None

MinBound=[403.341,33882.0162,131936.8323]

MaxBound=[33882.0162,131936.8323,980586.2055]

Boundary=zip(MinBount,MaxBound)

OutputSets=[]

for j in Boundary: 
    '''Note i switched these loops to group the sets by boundary'''
    TemporarySet=[]
    for i in TotalObs:
        if (WidthBounds(i,j)!=None):
            TemporarySet.append(i)
            print i, " is between", j[0]," and ",j[1]   
    OutputSets.append(TemporarySet)

这会将符合配置文件的每个项目附加到临时列表中 将该set附加到输出集列表。

for i in range(0,len(OutputSets)):
    print Boundary[i], "=",OutputSets[i]