我的代码是
montly_ratio = [0.046644865594960915, 0.060708276768318435, 0.07706787106693493, 0.09332535512669753, 0.10693770921153412, 0.11542007554770539, 0.11681104031351618, 0.11013791562902592, 0.09588202154576116, 0.07644396803642538, 0.056608936984611016, 0.044011964174509026]
len = 12
production_by_yr = [12129.0, 24197.0, 36205.0, 48152.0, 60038.0, 71863.0, 83628.0, 95331.0, 106974.0, 118556.0, 130077.0, 141538.0, 152938.0, 164277.0, 175555.0, 186772.0, 197929.0, 209024.0]
len = 18
我想做
for yr in production_by_yr:
for month in montly_ratio:
print yr * month,
这会给我 216 值
我按照以下列表理解做了同样的事情:
[yr * month for yr in production_by_yr for month in montly_ratio]
这给了我 216 的值,比如
565.7555748012809, 736.3306889229343, 934.7562081708537, 1131.9432323317144, 1297.0474750266974, 1399.9300963181188, 1416.8011079626376, 1335.8627786644554, 1162.953039328537, 927.1888883138034, 686.6097966863471, 533.82111347262, 1128.6658128012693, 1468.9581729630013, 1864.8112762066244, 2258.1936180007, 2587.571749791491, 2792.8195680278272, 2826.476742466151, 2665.00714447554, 2320.0572753427828, 1849.7146945773848, 1369.7664482166329, 1064.957497130595, 1688.77735886556, 2197.943160396969, 2790.242271978379, 3378.844482362084, 3871.6797620035927, 4178.783835204674, 4229.1437145508535, 3987.543235348883, 3471.4085900642826, 2767.6538627587806, 2049.526563527842, 1593.4531629380992, 2246.043568128558, 2923.2249429480694, 3710.9721276150503, 4493.8025000607395, 5149.264573953791, 5557.70747777311, 5624.685213176431, 5303.360913368856, 4616.911101471491, 3680.9299488899546, 2725.8335336829896, 2119.2640989309584, 2800.464440590263, 3644.803520616302, 4627.000843116639, 5603.067671096666, 6420.326185642085, 6929.590495733136, 7013.101238342884, 6612.460178535458, 5756.564809564408, 4589.542952970907, 3398.687358682076, 2642.390305109173, 3352.0399762506763, 4362.678893401668, 5538.328418483145, 6706.6399954698645, 7684.8645970684765, 8294.432889084752, 8394.391790050213, 7914.841030848689, 6890.369714343034, 5493.492875001637, 4068.0880385251016, 3162.831781472742, 3900.8168199753914, 5076.911769580934, 6445.031921585634, 7804.612798535461, 8942.986745942175, 9652.350077903506, 9768.67367933873, 9210.61360822418, 8018.421697828914, 6392.856158950181, 4734.09218214905, 3680.6325399858406, 4446.701682033219, 5787.380732600564, 7346.9572166819735, 8896.799429583201, 10194.47875684476, 11003.111222038302, 11135.71328412781, 10499.55763483067, 9140.528995978957, 7287.4799168804675, 5396.586571679953, 4195.70455672012, 4989.787852155348, 6494.207199014097, 8244.258439514297, 9983.386539323341, 11439.55450519465, 12346.947161640237, 12495.74422649808, 11781.893386499418, 10256.883372836253, 8177.517036728568, 6055.6844249917785, 4708.135855603929, 5530.028685476186, 7197.33046054476, 9136.858522211536, 11064.280802400752, 12678.107053282638, 13683.74247663376, 13848.649695409224, 13057.510725314796, 11367.38894637926, 9062.891074526447, 6711.329133147544, 5217.882424673092, 6067.424181995731, 7896.750517192557, 10024.757464773695, 12139.482218815434, 13910.136401108723, 15013.497167018873, 15194.429690861243, 14326.409651276805, 12472.045716607974, 9943.602030274104, 7363.520696147247, 5724.94426392761, 6602.020986579578, 8592.528077234254, 10908.032335071835, 13209.084113922514, 15135.749486382116, 16336.326652871126, 16533.201023894453, 15588.700302301071, 13570.949565543942, 10819.726347939575, 8012.315722927874, 6229.365385331658, 7133.7724543621325, 9284.602432393085, 11786.606065234893, 14272.993162366865, 16354.839371393604, 17652.11551411497, 17864.846883468537, 16844.272540471964, 14664.00461116562, 11691.187583554824, 8657.65760455244, 6731.101776921061, 7662.678585343394, 9972.973582669048, 12660.478655262868, 15331.20936414849, 17567.40605614319, 18960.8637507504, 19189.367269583498, 18093.126365789492, 15751.210853473005, 12557.985737119852, 9299.546341020943, 7230.153438695819, 8188.739379523363, 10657.641528062142, 13529.65010515576, 16383.732719267384, 18773.44954063087, 20262.57136277742, 20506.762182239334, 19335.261778253644, 16832.5682924661, 13420.120808634658, 9937.981932333387, 7726.520370655932, 8711.95483690204, 11338.60626857237, 14394.12041491357, 17430.56322772355, 19972.96982485665, 21557.238350196032, 21817.031621436043, 20570.67877786443, 17908.076928144903, 14277.59279809924, 10572.964378489769, 8220.2025728014, 9232.37160234502, 12015.9285124765, 15253.966652407364, 18471.794214872116, 21166.073846529736, 22844.98013308178, 23120.292398213944, 21799.48750253747, 18977.83264253096, 15130.478149481638, 11204.550288427074, 8711.244057096397, 9749.89638612111, 12689.486843220993, 16109.034681895006, 19507.239030002824, 22352.54773023171, 24125.565871283572, 24416.310890492405, 23021.467676441513, 20041.64367158118, 15978.623974845777, 11832.626444271333, 9199.556799612574]
所以我想验证最后12个数字是
production_yr = 209024.0 and ratio being same
我做了
[20924.0 * r for r in montly_ratio] [975.9971677089621,1270.259983100295,1612.5681342045464,1952.739730671019, 2237.56462754214,2415.0496607601876,2444.5542075200123,2304.525746621738,2006.2354188235065,1599.5135871941645,1184.485397466001,920.9063383874269]
然后我意识到我用嵌套for循环实现的数字非常大并且可能不正确
9749.89638612111, 12689.486843220993, 16109.034681895006, 19507.239030002824, 22352.54773023171, 24125.565871283572, 24416.310890492405, 23021.467676441513, 20041.64367158118, 15978.623974845777, 11832.626444271333, 9199.556799612574
我在这里做的不是什么?
答案 0 :(得分:6)
错误,但您的验证代码是错误的:
209024.0
是正确的值,您使用了:
[20924.0 * r for r in montly_ratio]
^^^^^