与2D列表中的元素组合的总和

时间:2017-08-17 19:00:55

标签: python-2.7

我有一个清单

A=[[..],[…],...[……]]

所以每个元素都可以得到A [i] [j]。 我的任务: 我需要在A[i][j]上使用每个子列表中不同的i组合构建总和j。例如,澄清: 我们是

 array([[     0.  ,    157.31,    314.62,    471.93,    629.24,    786.55,
           943.86,   1101.17,   1258.48],
       [     0.  ,    207.99,    415.98,    623.97,    831.96,   1039.95,
          1247.94,   1455.93,   1663.92],
       [     0.  ,    209.17,    418.34,    627.51,    836.68,   1045.85,
          1255.02,   1464.19,   1673.36],
       [     0.  ,    293.49,    586.98,    880.47,   1173.96,   1467.45,
          1760.94,   2054.43,   2347.92],
       [     0.  ,    299.2 ,    598.4 ,    897.6 ,   1196.8 ,   1496.  ,
          1795.2 ,   2094.4 ,   2393.6 ],
       [     0.  ,    310.42,    620.84,    931.26,   1241.68,   1552.1 ,
          1862.52,   2172.94,   2483.36],
       [     0.  ,    316.85,    633.7 ,    950.55,   1267.4 ,   1584.25,
          1901.1 ,   2217.95,   2534.8 ],
       [     0.  ,    337.12,    674.24,   1011.36,   1348.48,   1685.6 ,
          2022.72,   2359.84,   2696.96],
       [     0.  ,    359.25,    718.5 ,   1077.75,   1437.  ,   1796.25,
          2155.5 ,   2514.75,   2874.  ],
       [     0.  ,    360.46,    720.92,   1081.38,   1441.84,   1802.3 ,
          2162.76,   2523.22,   2883.68],
       [     0.  ,    396.32,    792.64,   1188.96,   1585.28,   1981.6 ,
          2377.92,   2774.24,   3170.56],
       [     0.  ,    422.06,    844.12,   1266.18,   1688.24,   2110.3 ,
          2532.36,   2954.42,   3376.48],
       [     0.  ,    429.31,    858.62,   1287.93,   1717.24,   2146.55,
          2575.86,   3005.17,   3434.48],
       [     0.  ,    442.97,    885.94,   1328.91,   1771.88,   2214.85,
          2657.82,   3100.79,   3543.76],
       [     0.  ,    481.07,    962.14,   1443.21,   1924.28,   2405.35,
          2886.42,   3367.49,   3848.56],
       [     0.  ,    637.9 ,   1275.8 ,   1913.7 ,   2551.6 ,   3189.5 ,
          3827.4 ,   4465.3 ,   5103.2 ],
       [     0.  ,    653.72,   1307.44,   1961.16,   2614.88,   3268.6 ,
          3922.32,   4576.04,   5229.76],
       [     0.  ,    678.88,   1357.76,   2036.64,   2715.52,   3394.4 ,
          4073.28,   4752.16,   5431.04],
       [     0.  ,    684.7 ,   1369.4 ,   2054.1 ,   2738.8 ,   3423.5 ,
          4108.2 ,   4792.9 ,   5477.6 ],
       [     0.  ,    692.57,   1385.14,   2077.71,   2770.28,   3462.85,
          4155.42,   4847.99,   5540.56],
       [     0.  ,    710.33,   1420.66,   2130.99,   2841.32,   3551.65,
          4261.98,   4972.31,   5682.64],
       [     0.  ,    710.93,   1421.86,   2132.79,   2843.72,   3554.65,
          4265.58,   4976.51,   5687.44],
       [     0.  ,    713.33,   1426.66,   2139.99,   2853.32,   3566.65,
          4279.98,   4993.31,   5706.64],
       [     0.  ,    713.8 ,   1427.6 ,   2141.4 ,   2855.2 ,   3569.  ,
          4282.8 ,   4996.6 ,   5710.4 ],
       [     0.  ,    734.06,   1468.12,   2202.18,   2936.24,   3670.3 ,
          4404.36,   5138.42,   5872.48],
       [     0.  ,    738.42,   1476.84,   2215.26,   2953.68,   3692.1 ,
          4430.52,   5168.94,   5907.36],
       [     0.  ,    740.45,   1480.9 ,   2221.35,   2961.8 ,   3702.25,
          4442.7 ,   5183.15,   5923.6 ],
       [     0.  ,    746.87,   1493.74,   2240.61,   2987.48,   3734.35,
          4481.22,   5228.09,   5974.96],
       [     0.  ,    758.46,   1516.92,   2275.38,   3033.84,   3792.3 ,
          4550.76,   5309.22,   6067.68],
       [     0.  ,    786.75,   1573.5 ,   2360.25,   3147.  ,   3933.75,
          4720.5 ,   5507.25,   6294.  ],
       [     0.  ,    790.45,   1580.9 ,   2371.35,   3161.8 ,   3952.25,
          4742.7 ,   5533.15,   6323.6 ],
       [     0.  ,    792.37,   1584.74,   2377.11,   3169.48,   3961.85,
          4754.22,   5546.59,   6338.96],
       [     0.  ,    793.74,   1587.48,   2381.22,   3174.96,   3968.7 ,
          4762.44,   5556.18,   6349.92],
       [     0.  ,    795.67,   1591.34,   2387.01,   3182.68,   3978.35,
          4774.02,   5569.69,   6365.36],
       [     0.  ,    798.12,   1596.24,   2394.36,   3192.48,   3990.6 ,
          4788.72,   5586.84,   6384.96],
       [     0.  ,    808.54,   1617.08,   2425.62,   3234.16,   4042.7 ,
          4851.24,   5659.78,   6468.32],
       [     0.  ,    818.68,   1637.36,   2456.04,   3274.72,   4093.4 ,
          4912.08,   5730.76,   6549.44],
       [     0.  ,    850.3 ,   1700.6 ,   2550.9 ,   3401.2 ,   4251.5 ,
          5101.8 ,   5952.1 ,   6802.4 ],
       [     0.  ,    860.61,   1721.22,   2581.83,   3442.44,   4303.05,
          5163.66,   6024.27,   6884.88],
       [     0.  ,    861.84,   1723.68,   2585.52,   3447.36,   4309.2 ,
          5171.04,   6032.88,   6894.72],
       [     0.  ,    873.79,   1747.58,   2621.37,   3495.16,   4368.95,
          5242.74,   6116.53,   6990.32],
       [     0.  ,    901.6 ,   1803.2 ,   2704.8 ,   3606.4 ,   4508.  ,
          5409.6 ,   6311.2 ,   7212.8 ],
       [     0.  ,    906.67,   1813.34,   2720.01,   3626.68,   4533.35,
          5440.02,   6346.69,   7253.36],
       [     0.  ,    912.17,   1824.34,   2736.51,   3648.68,   4560.85,
          5473.02,   6385.19,   7297.36],
       [     0.  ,    914.54,   1829.08,   2743.62,   3658.16,   4572.7 ,
          5487.24,   6401.78,   7316.32],
       [     0.  ,    973.12,   1946.24,   2919.36,   3892.48,   4865.6 ,
          5838.72,   6811.84,   7784.96],
       [     0.  ,    976.23,   1952.46,   2928.69,   3904.92,   4881.15,
          5857.38,   6833.61,   7809.84],
       [     0.  ,    995.86,   1991.72,   2987.58,   3983.44,   4979.3 ,
          5975.16,   6971.02,   7966.88],
       [     0.  ,   1002.01,   2004.02,   3006.03,   4008.04,   5010.05,
          6012.06,   7014.07,   8016.08],
       [     0.  ,   1013.61,   2027.22,   3040.83,   4054.44,   5068.05,
          6081.66,   7095.27,   8108.88],
       [     0.  ,   1013.8 ,   2027.6 ,   3041.4 ,   4055.2 ,   5069.  ,
          6082.8 ,   7096.6 ,   8110.4 ],
       [     0.  ,   1024.13,   2048.26,   3072.39,   4096.52,   5120.65,
          6144.78,   7168.91,   8193.04],
       [     0.  ,   1029.78,   2059.56,   3089.34,   4119.12,   5148.9 ,
          6178.68,   7208.46,   8238.24],
       [     0.  ,   1082.17,   2164.34,   3246.51,   4328.68,   5410.85,
          6493.02,   7575.19,   8657.36],
       [     0.  ,   1086.54,   2173.08,   3259.62,   4346.16,   5432.7 ,
          6519.24,   7605.78,   8692.32],
       [     0.  ,   1089.62,   2179.24,   3268.86,   4358.48,   5448.1 ,
          6537.72,   7627.34,   8716.96],
       [     0.  ,   1096.45,   2192.9 ,   3289.35,   4385.8 ,   5482.25,
          6578.7 ,   7675.15,   8771.6 ],
       [     0.  ,   1173.85,   2347.7 ,   3521.55,   4695.4 ,   5869.25,
          7043.1 ,   8216.95,   9390.8 ],
       [     0.  ,   1177.28,   2354.56,   3531.84,   4709.12,   5886.4 ,
          7063.68,   8240.96,   9418.24],
       [     0.  ,   1195.49,   2390.98,   3586.47,   4781.96,   5977.45,
          7172.94,   8368.43,   9563.92],
       [     0.  ,   1216.53,   2433.06,   3649.59,   4866.12,   6082.65,
          7299.18,   8515.71,   9732.24],
       [     0.  ,   1226.  ,   2452.  ,   3678.  ,   4904.  ,   6130.  ,
          7356.  ,   8582.  ,   9808.  ],
       [     0.  ,   1237.51,   2475.02,   3712.53,   4950.04,   6187.55,
          7425.06,   8662.57,   9900.08],
       [     0.  ,   1266.34,   2532.68,   3799.02,   5065.36,   6331.7 ,
          7598.04,   8864.38,  10130.72],
       [     0.  ,   1270.44,   2540.88,   3811.32,   5081.76,   6352.2 ,
          7622.64,   8893.08,  10163.52],
       [     0.  ,   1286.56,   2573.12,   3859.68,   5146.24,   6432.8 ,
          7719.36,   9005.92,  10292.48],
       [     0.  ,   1325.26,   2650.52,   3975.78,   5301.04,   6626.3 ,
          7951.56,   9276.82,  10602.08],
       [     0.  ,   1359.51,   2719.02,   4078.53,   5438.04,   6797.55,
          8157.06,   9516.57,  10876.08],
       [     0.  ,   1393.41,   2786.82,   4180.23,   5573.64,   6967.05,
          8360.46,   9753.87,  11147.28],
       [     0.  ,   1395.41,   2790.82,   4186.23,   5581.64,   6977.05,
          8372.46,   9767.87,  11163.28],
       [     0.  ,   1400.79,   2801.58,   4202.37,   5603.16,   7003.95,
          8404.74,   9805.53,  11206.32],
       [     0.  ,   1423.86,   2847.72,   4271.58,   5695.44,   7119.3 ,
          8543.16,   9967.02,  11390.88],
       [     0.  ,   1447.72,   2895.44,   4343.16,   5790.88,   7238.6 ,
          8686.32,  10134.04,  11581.76],
       [     0.  ,   1448.9 ,   2897.8 ,   4346.7 ,   5795.6 ,   7244.5 ,
          8693.4 ,  10142.3 ,  11591.2 ],
       [     0.  ,   1452.13,   2904.26,   4356.39,   5808.52,   7260.65,
          8712.78,  10164.91,  11617.04],
       [     0.  ,   1455.65,   2911.3 ,   4366.95,   5822.6 ,   7278.25,
          8733.9 ,  10189.55,  11645.2 ],
       [     0.  ,   1478.98,   2957.96,   4436.94,   5915.92,   7394.9 ,
          8873.88,  10352.86,  11831.84],
       [     0.  ,   1541.85,   3083.7 ,   4625.55,   6167.4 ,   7709.25,
          9251.1 ,  10792.95,  12334.8 ],
       [     0.  ,   1542.41,   3084.82,   4627.23,   6169.64,   7712.05,
          9254.46,  10796.87,  12339.28],
       [     0.  ,   1554.69,   3109.38,   4664.07,   6218.76,   7773.45,
          9328.14,  10882.83,  12437.52],
       [     0.  ,   1559.04,   3118.08,   4677.12,   6236.16,   7795.2 ,
          9354.24,  10913.28,  12472.32],
       [     0.  ,   1574.32,   3148.64,   4722.96,   6297.28,   7871.6 ,
          9445.92,  11020.24,  12594.56],
       [     0.  ,   1595.75,   3191.5 ,   4787.25,   6383.  ,   7978.75,
          9574.5 ,  11170.25,  12766.  ],
       [     0.  ,   1607.69,   3215.38,   4823.07,   6430.76,   8038.45,
          9646.14,  11253.83,  12861.52],
       [     0.  ,   1644.48,   3288.96,   4933.44,   6577.92,   8222.4 ,
          9866.88,  11511.36,  13155.84],
       [     0.  ,   1648.06,   3296.12,   4944.18,   6592.24,   8240.3 ,
          9888.36,  11536.42,  13184.48],
       [     0.  ,   1656.44,   3312.88,   4969.32,   6625.76,   8282.2 ,
          9938.64,  11595.08,  13251.52],
       [     0.  ,   3196.25,   6392.5 ,   9588.75,  12785.  ,  15981.25,
         19177.5 ,  22373.75,  25570.  ],
       [     0.  ,   3196.46,   6392.92,   9589.38,  12785.84,  15982.3 ,
         19178.76,  22375.22,  25571.68],
       [     0.  ,   3196.55,   6393.1 ,   9589.65,  12786.2 ,  15982.75,
         19179.3 ,  22375.85,  25572.4 ],
       [     0.  ,   3196.77,   6393.54,   9590.31,  12787.08,  15983.85,
         19180.62,  22377.39,  25574.16],
       [     0.  ,   3230.92,   6461.84,   9692.76,  12923.68,  16154.6 ,
         19385.52,  22616.44,  25847.36],
       [     0.  ,   3231.27,   6462.54,   9693.81,  12925.08,  16156.35,
         19387.62,  22618.89,  25850.16],
       [     0.  ,   3249.88,   6499.76,   9749.64,  12999.52,  16249.4 ,
         19499.28,  22749.16,  25999.04],
       [     0.  ,   3250.1 ,   6500.2 ,   9750.3 ,  13000.4 ,  16250.5 ,
         19500.6 ,  22750.7 ,  26000.8 ],
       [     0.  ,   3252.07,   6504.14,   9756.21,  13008.28,  16260.35,
         19512.42,  22764.49,  26016.56],
       [     0.  ,   3252.12,   6504.24,   9756.36,  13008.48,  16260.6 ,
         19512.72,  22764.84,  26016.96],
       [     0.  ,   3267.15,   6534.3 ,   9801.45,  13068.6 ,  16335.75,
         19602.9 ,  22870.05,  26137.2 ],
       [     0.  ,   3267.17,   6534.34,   9801.51,  13068.68,  16335.85,
         19603.02,  22870.19,  26137.36],
       [     0.  ,   3550.84,   7101.68,  10652.52,  14203.36,  17754.2 ,
         21305.04,  24855.88,  28406.72],
       [     0.  ,   3593.12,   7186.24,  10779.36,  14372.48,  17965.6 ,
         21558.72,  25151.84,  28744.96]])

在这里,我有i=0..3,但每个子列表中j的更改区域不同。

W=A[0,j{0}]+A[1,j{1}]+A[2,j[2}]+A[3,j{3}]

我需要使用Wj{0}j{1}j{2}的不同值获取所有j{3}。我怎样才能更快地使用python? 谢谢!

1 个答案:

答案 0 :(得分:1)

itertools.product在这里非常有帮助:

import itertools

A = [[0, 1, 2, 3], [0, 1, 2], [0, 1, 2, 3, 4, 5,], [0, 1]]

#I changed this to be a generator so it doesn't use all memory at once
#the change is from square brackets '[...]' to parens '(...)'
sums = ((vs,sum(vs)) for vs in itertools.product(*A))

for k,v in sums:
    print k,'=',v

所有工作都在sums =...行中进行,其中i A元素的每个组合都汇总在一起。我不确定你想要的最终输出是什么,所以我只是按顺序保留它并返回一个总和值的总和,以及它们的总和。

输出:

(0, 0, 0, 0) = 0
(0, 0, 0, 1) = 1
(0, 0, 1, 0) = 1
(0, 0, 1, 1) = 2
(0, 0, 2, 0) = 2
(0, 0, 2, 1) = 3
...
(3, 2, 4, 1) = 10
(3, 2, 5, 0) = 10
(3, 2, 5, 1) = 11