优化python中大型(ish)字典的嵌套for循环追加

时间:2018-01-08 20:15:18

标签: python dictionary for-loop

我想知道这段代码是否有更快更有效的方式:

obstacles = [[(e.x, e.y) for e in ei] for ei in entities_by_distance.values()]

其中entities_by_distance是字典是有序的字典值。 注意 - 海量数字和小数是键,[实体...]是一个__str __函数的对象:

(example)
entities: OrderedDict([(2.550572674065858, [Entity Ship (id: 2) at position: (x = 90.66311896062463, y = 72.11449676604731), with radius = 0.5]), 
(2.5913562453639853, [Entity Ship (id: 1) at position: (x = 89.86260859321298, y = 67.03537860237056), with radius = 0.5]),
(19.961556716594657, [Entity Planet (id: 9) at position: (x = 105.23024143493308, y = 82.82278490432245), with radius = 6.0215941952715575]), 
(46.76771252214029, [Entity Planet (id: 20) at position: (x = 82.30382160250116, y = 115.67442891727431), with radius = 4.488507686331526]), 
(52.3661847794911, [Entity Planet (id: 21) at position: (x = 139.29669002554886, y = 51.15678819131277), with radius = 4.488507686331526]), 
(53.47382276913792, [Entity Planet (id: 17) at position: (x = 51.82322745486708, y = 106.74477720484593), with radius = 6.862376008348898]), 
(54.47817838264437, [Entity Planet (id: 7) at position: (x = 109.91984600906738, y = 120.40840384655024), with radius = 5.028365022567388]), 
(65.48032125358313, [Entity Planet (id: 12) at position: (x = 155.70489249905236, y = 72.35721763065746), with radius = 6.862376008348898]), 
(84.64499262771506, [Entity Planet (id: 2) at position: (x = 159.00691825401316, y = 119.00691825401316), with radius = 7.273282648725352]), 
(86.46105264739788, [Entity Planet (id: 6) at position: (x = 132.16648183917027, y = 145.23158833572938), with radius = 5.028365022567388]), 
(97.82662838079216, [Entity Planet (id: 16) at position: (x = 66.11833495581483, y = 164.3875595741884), with radius = 6.862376008348898]), 
(99.10114497773336, [Entity Planet (id: 1) at position: (x = 159.00691825401316, y = 140.99308174598684), with radius = 7.273282648725352]), 
(101.83154951011286, [Entity Planet (id: 8) at position: (x = 30.282862985877983, y = 151.87030808690568), with radius = 6.0215941952715575]), 
(103.29626428630006, [Entity Planet (id: 3) at position: (x = 180.99308174598684, y = 119.00691825401316), with radius = 7.273282648725352]), 
(115.43994271679512, [Entity Planet (id: 0) at position: (x = 180.99308174598684, y = 140.99308174598684), with radius = 7.273282648725352]), 
(123.02929038083357, [Entity Ship (id: 7) at position: (x = 91.97336288664222, y = 192.6100901992336), with radius = 0.5]), 
(125.2971707367115, [Entity Ship (id: 6) at position: (x = 91.99893386609475, y = 194.877833154939), with radius = 0.5]),
(125.93253313072393, [Entity Planet (id: 4) at position: (x = 207.83351816082967, y = 114.76841166427062), with radius = 5.028365022567388]), 
(126.97519491854682, [Entity Planet (id: 19) at position: (x = 113.0071315769523, y = 194.51764072596157), with radius = 4.488507686331526]), 
(127.56536587638557, [Entity Ship (id: 8) at position: (x = 91.94782306148926, y = 197.14691459616395), with radius = 0.5]),
(136.77166985003248, [Entity Planet (id: 22) at position: (x = 226.99286842304767, y = 65.4823592740384), with radius = 4.488507686331526]), 
(150.91114011338163, [Entity Planet (id: 15) at position: (x = 184.29510750094764, y = 187.64278236934254), with radius = 6.862376008348898]), 
(156.34301739158659, [Entity Planet (id: 5) at position: (x = 230.08015399093256, y = 139.59159615344979), with radius = 5.028365022567388]), 
(157.75904558106072, [Entity Ship (id: 5) at position: (x = 248.0266371133578, y = 67.38990980076639), with radius = 0.5]),
(157.78143704978217, [Entity Ship (id: 3) at position: (x = 248.00106613390525, y = 65.12216684506099), with radius = 0.5]), 
(157.91308408229256, [Entity Ship (id: 4) at position: (x = 248.05217693851074, y = 62.85308540383603), with radius = 0.5]), 
(177.71729527916176, [Entity Planet (id: 18) at position: (x = 200.70330997445112, y = 208.84321180868724), with radius = 4.488507686331526]), 
(180.14139590132814, [Entity Planet (id: 11) at position: (x = 234.76975856506695, y = 177.17721509567755), with radius = 6.0215941952715575]), 
(183.3363799590737, [Entity Planet (id: 23) at position: (x = 257.69617839749884, y = 144.3255710827256), with radius = 4.488507686331526]), 
(185.4333403614214, [Entity Planet (id: 13) at position: (x = 273.8816650441852, y = 95.6124404258116), with radius = 6.862376008348898]), 
(198.22059101099785, [Entity Ship (id: 10) at position: (x = 249.33688103937536, y = 187.88550323395268), with radius = 0.5]), 
(200.03881506501224, [Entity Ship (id: 9) at position: (x = 249.7170329384406, y = 190.40758679706644), with radius = 0.5]), 
(201.92607245618254, [Entity Ship (id: 11) at position: (x = 250.137391406787, y = 192.96462139762946), with radius = 0.5]), 
(214.85209786649642, [Entity Planet (id: 14) at position: (x = 288.17677254513285, y = 153.25522279515417), with radius = 6.862376008348898]), 
(222.7924522804907, [Entity Planet (id: 10) at position: (x = 309.717137014122, y = 108.12969191309428), with radius = 6.0215941952715575])])

也许使用numpy但我不知道如何。我正在使用cython编译,如果这有助于一些优化。

0 个答案:

没有答案