0
\$\begingroup\$

I am trying to analyse my running data, which comes in gpx form, import and writing of data is working wonderfully.

Now I want to average the pace I calculated between two points overage a certain distance (for example 1000 m) and see the result as a list for each individual kilometer. If the kilometer is not finished at the end, I want the calculate the pace in the last segment as well.

The function has the following input:

average_over_distance(input_datalist, input_distancelist, average_distance)
    input_datalist = [0, 5.3, 4.3, 4.6 ... ]
    input_distancelist = [0, 4.5, 7.8, 12.3 ... ]
    average_distance = 1000

Where input_datalist is the pace at each datapoint and input_distancelist is the amount of meters covered from the beginning to this datapoint. I have already written some code which is working, but is surprisingly slow and has a minor bug, in which the last item needs to be removed, as it will always be a zero.

If someone could take a look at it and suggest some improvements (especially in terms of performance), that would be very helpful.

Typically, the files contain between 800 and 3000 datapoints. For a example, for a run of roughly 6 km "runtastic_20191117_1510", the input data is:

input_datalist = [0, 8.154297259068265, 7.699997924184752, 4.1290248509679355, 5.233554408345463, 3.736102600544462, 4.330745941935702, 5.230497758127484, 5.121644868994811, 4.015323039710534, 4.726542792381121, 3.740745581335375, 3.4118823620200422, 3.987048386430994, 3.7203695740533966, 4.410022185697186, 5.2436728280589415, 3.7116435736709072, 4.092616264885011, 4.223133016298036, 3.6989551972321575, 3.933683921646469, 5.299174480076175, 5.454088851201578, 4.885582841700339, 4.322790700547173, 5.077202464669111, 4.468306638956414, 5.10435062743631, 4.611544695366803, 4.276830319696586, 4.088270186235909, 3.9011269060681983, 5.041657604796356, 4.277273006103617, 4.406055316530598, 4.557975009670055, 4.406041751786603, 4.27732553606914, 4.745072973440513, 4.710883275762832, 4.640505059675511, 5.06935043569426, 4.490555199849174, 4.572841974069774, 5.045738092463033, 4.205832799143788, 4.505775599108849, 4.28371867045946, 4.263979223210943, 4.7588714454922725, 4.91203496026689, 4.696805096033936, 4.162071809512042, 4.535661818149418, 4.171066245852658, 4.143397024221024, 4.640261191080346, 4.399112208437336, 5.685874427165761, 4.505487986404551, 4.720550515635307, 4.846515601858323, 5.298586073902652, 4.696656283201031, 4.441343777394891, 5.337965772042491, 4.48485689638503, 5.769112947432504, 5.319572026762231, 5.116981287986407, 5.22259033813319, 4.853202942230398, 4.448933861887161, 4.789873410985746, 5.672882596467915, 5.5417906688838166, 6.457016910025886, 6.786483505021053, 5.198308170917069, 5.092476666244364, 4.111794944472009, 4.62311212962504, 4.936303165196316, 4.2158075941616495, 5.0052319036352255, 6.027735861446753, 4.451769206323267, 4.348499095726568, 4.716995991496699, 4.739598104126194, 3.803071758523529, 3.8735972911018113, 4.655091267145741, 5.07588762499882, 5.502452004107536, 4.90400116659635, 5.053913295715235, 4.58068413622591, 4.816772523066801, 5.8804125927899475, 4.6149157805783245, 4.633021535424502, 4.694860556377299, 4.162950942798815, 4.143781839386801, 4.210068875540942, 4.466337101965024, 4.806729632448732, 4.497942736910237, 4.936378303939041, 4.767575030726392, 4.467464723983651, 4.754562019764752, 4.434949315331764, 3.7991288006306982, 4.356167584044449, 4.899659810097845, 4.824108634407948, 4.634182253538913, 4.9030628045839, 4.820284931682572, 4.542769711287221, 5.007690356986818, 5.295531279293059, 4.501087763083377, 4.488216232376019, 3.639894520745288, 4.905012199105443, 5.343166287117647, 4.4175484463040045, 4.525020534675778, 4.670701777770286, 3.941406726590055, 4.655011763949892, 6.140477088073999, 7.0078624375573995, 5.545091724531036, 5.288132248589611, 4.8816938060736526, 4.419245209980266, 4.7858786837636815, 4.956721947840855, 3.8419543236293525, 4.584358093975693, 3.6157730397543, 3.679531116433433, 3.8551332359568007, 5.01671434928462, 6.037315646638342, 4.200679939449504, 5.553716753945233, 4.8052903165724645, 5.62792540716035, 4.496031477404894, 3.3585767502392505, 4.426144671921158, 4.16635108522084, 4.543574826368433, 4.023584939710059, 4.4773033732429965, 4.056618759180805, 4.288378568522181, 4.016701064525987, 3.6309957856654416, 4.68751694219872, 4.200361225258804, 4.4642917645643445, 4.611021161957354, 4.608897935198266, 4.375182381141827, 4.3219335803397305, 4.981445645654108, 4.833516446559961, 4.336397398785247, 4.203682658060733, 3.784297284926399, 4.424894795321563, 4.088044358642628, 5.20713570393957, 6.0627377944839695, 3.735901221369735, 3.772403843344582, 4.9605949817268975, 6.184966776447468, 5.575863638814157, 3.965755781299144, 5.054754982298068, 5.055491060388767, 4.734832940836844, 4.6680405144198405, 4.518231790785486, 5.352054360203661, 3.933386810266203, 4.073500276584424, 3.6496806936814843, 4.160556562775662, 4.231158566179572, 4.126957725155156, 5.593731232209662, 6.557381777970839, 4.642808962883382, 3.955271250935829, 4.328041504215481, 3.8714835731936508, 4.617792757235988, 4.316975631278537, 4.639702196406146, 3.816561586325325, 4.071972083203815, 4.105020127529573, 5.418019364469931, 5.979515822345107, 4.572346964592393, 5.072186750588375, 4.605953060201322, 4.52944254464933, 5.45304404918759, 4.214181255790554, 5.065635658544667, 4.646521009104833, 3.766190012573553, 4.747353072646974, 3.9823024273203877, 4.008735954584001, 4.498706648060665, 4.335661856552447, 5.4356808518186295, 5.975294162959008, 6.343006329478395, 4.713236251049612, 6.188658480557391, 5.124416742172999, 5.619545161380488, 4.049275101887822, 4.454425726867608, 4.429777695304846, 4.573400986105693, 3.7655872110826647, 6.033389303976118, 5.409110541159138, 4.522967609924991, 3.8649484408364314, 3.547691073057468, 3.8216406348013447, 3.7081335110051663, 5.148494271933852, 5.0951639784476335, 5.1868397841911404, 3.915056916229462, 4.406991109627597, 3.9763203810458996, 4.28549379742172, 4.054115252910068, 4.776024139760099, 5.520744481502811, 4.888183596040763, 4.124802660905125, 4.864276535037368, 5.177312990215, 4.86043806706891, 4.135061226941825, 3.570930298137865, 3.700051516827064, 4.826854581084116, 4.878739859154471, 3.926577500254844, 4.101678697318054, 5.533643097300404, 5.603894439072506, 5.5836491017136645, 4.132822241747506, 4.327310749268595, 4.901185832428964, 4.404933049474804, 4.353807464674405, 4.005766544385017, 4.506879230892712, 4.906734585665481, 5.123709595337477, 3.9234155679802045, 4.188877024821228, 4.932978861075624, 4.727035002624785, 4.6932702535094615, 5.883208720833115, 5.60566192288835, 4.560563955425492, 5.669761784806086, 5.275740020308531, 3.8030108229859043, 3.262000519242214, 4.002520224062654, 4.718094076517959, 4.478663977565892, 4.390544097105828, 4.19771623465651, 4.5480069304831785, 4.594856759883293, 3.8475266675044115, 4.929983554415432, 4.382543120384308, 3.6614842492966537, 4.101811441660566, 4.5657701468204115, 4.258297327206018, 6.2327786523192685, 6.141584317657115, 4.330490431701469, 3.6629381384362287, 3.6957939483910436, 5.121599558473292, 6.042316399492867, 4.75216860990142, 5.978575732467344, 6.00395603245871, 8.042208384599991, 5.1021107749039025, 4.517556660128847, 4.935516587555954, 5.632047276257036, 6.423600308741078, 6.00440093918871, 4.845324684406579, 5.055066910344564, 4.9389227035720875, 4.680938244648393, 4.9204120831447336, 4.678421499818818, 4.177438981630106, 4.353078428831223, 4.250867676745693, 4.968408209906815, 4.407893575868392, 4.216143959716419, 4.073363509236613, 3.5906037538507043, 3.691974738042348, 5.036374082670621, 5.303505234368339, 4.595696918891344, 4.1300635347916135, 4.578008133895794, 4.8718892710481585, 4.910747946403624, 3.906923185712786, 3.7330877569388266, 3.897689856046975, 5.802573172588164, 4.008559710782361, 4.392256205143359, 4.748812027698272, 4.07431196233908, 5.260360061285357, 9.359585548001148, 13.125105261196474, 5.380398263912016, 4.130980041632252, 3.2739945880797694, 3.928870358141829, 4.365717934322635, 4.310671755409893, 4.29005790128022, 4.707968241314882, 6.345982614926924, 6.131622489289469, 5.076782565165626, 4.865378764540353, 4.119554065730303, 4.124088634945004, 5.238130249873582, 5.579928645245936, 5.522380159660524, 5.121673937886574, 5.900959324337007, 4.858015036866666, 5.187047039414815, 4.879726213161024, 4.091664395550873, 4.837767172037724, 4.257922821251948, 4.67990874895055, 5.406604825715869, 5.579957583303129, 4.6204975750040305, 5.238908190956787, 4.6220244661279235, 4.865310773483241, 5.3436658704418, 4.7078411709655885, 5.522251283593757, 5.809488123591483, 5.857531981615105, 5.414726666730081, 5.08323718554349, 5.945605891365354, 5.607541690637272, 4.133746420689716, 4.135401205340503, 5.224387695688467, 5.228290701546441, 5.538561186853402, 5.446367710527512, 4.6423440977698816, 4.853491793936539, 4.607173624287225, 6.040282575425058, 5.186394503344894, 4.849786217055432, 4.8537653697490395, 4.776660374217542, 5.032812532554486, 5.53873330719436, 5.34336505487213, 4.322361773758633, 4.360275788289467, 4.6084726720321365, 4.769621387307082, 4.134098145663927, 4.901530424054935, 5.152005831962173, 5.487658037299265, 4.719588500446772, 4.870480538873378, 4.902862677085294, 4.620710345607096, 5.2355052862068945, 5.199863591781361, 5.766644251221267, 7.598788637776362, 7.133574355872142, 5.964849017616679, 5.598263679775067, 5.612157326648779, 4.8704106527246545, 4.532958071342506, 4.13961046938866, 6.027068718261925, 5.237024579091238, 5.102538323259633, 5.210318277513763, 3.778892100015756, 5.456404694376721, 4.8019619965987985, 4.878191938028389, 6.548044633747711, 5.768001662343498, 5.045591480188853, 4.720907855592685, 9.172196383817367, 5.2545597237758095, 4.681417777704229, 5.971315496784966, 3.684618597555957, 4.801114328452435, 4.680478088477566, 5.555867415427176, 5.27830765541006, 4.372746909313776, 4.125134248904619, 4.949176280889171, 5.195891869097251, 4.573216598940454, 9.425778187394444, 14.530948232708237, 15.593075826191377, 6.853818862628895, 7.0628011743550605, 6.877345450766101, 4.419415731769701, 4.365855944753687, 4.914215523401811, 5.890169505133561, 6.696349144225213, 5.455641272726224, 4.491935561060528, 5.045477445700691, 4.366653603581333, 4.667168036633596, 5.025731647884249, 5.452353090157114, 5.646332078214436, 5.675451738912615, 4.926132948232751, 4.853437603310502, 5.8863644009082, 6.872906850875094, 5.061691654041292, 4.020451529830195, 4.759392869964344, 4.642903720505812, 4.16820169937677, 3.8732898979294474, 4.591339181786737, 4.77128676701023, 4.394301688749651, 3.5827652905733913, 3.596804035306872, 3.6349118606187116, 4.187996663638694, 4.59958514622361, 4.467786796658763, 5.33258780981264, 5.446587239227867, 5.400921522256128, 4.994677705240711, 5.280729520522615, 5.596505350164431, 5.480381222725426, 5.276506641778495, 4.927198227898725, 5.5007019344073, 6.3927226627934415, 4.893321707184026, 5.469610714372455, 5.480731726644543, 5.809071390656785, 5.344346764147256, 8.784779754402654, 5.64251223436406, 5.200250666831702, 4.813288290089864, 4.204272390177281, 4.597767286104216, 5.542276227351737, 5.159232715823571, 4.97802276508083, 5.168328146529712, 5.869873943518415, 4.7577037646608495, 4.5594698514444385, 4.6486154469728485, 4.361110487388936, 4.445216263055807, 4.735657362747071, 5.855672069800528, 6.711408602885964, 6.277830805653751, 8.069971890956039, 6.523580156999423, 5.352097060441109, 4.5193744659476565, 4.670852116926712, 4.31749071218104, 5.9038388477506745, 5.156765629067522, 5.972383637211377, 4.863313642890077, 4.741459973381345, 5.013307214865355, 3.756380102752061, 3.6573548432696996, 3.9542882382964577, 4.846262971576997, 4.628820794382232, 4.634623072909339, 6.663578270926263, 5.1653246237832455, 4.057144047002918, 4.441839529076168, 4.847284940314969, 6.549024398459078, 5.516858945171008, 5.050030579321399, 7.508700305495534, 5.289871902784273, 7.261676530493964, 6.273114030218404, 5.891465813903992, 5.754975587418065, 5.611277253106119, 3.974445324538755, 3.866515171739321, 3.8148723957045867, 4.331822088552902, 4.236060975349821, 3.8929893685590025, 4.273716116286058, 5.122359735401137, 5.3881856603291105, 6.168689308793071, 7.496764045470881, 5.355983664117728, 4.457117100223586, 4.519838111870202, 4.76018042036791, 5.296781862809513, 5.209190890406699, 6.780529324328974, 5.735678685893203, 4.885659099536959, 4.295766299476571, 5.461881703955669, 4.234657086287799, 4.104718261660021, 5.047180653459006, 4.093515224120995, 3.5354341296045817, 4.024620321747599, 4.588147410590329, 5.051399703901492, 5.247352666742633, 4.684998126730578, 4.992099870242227, 4.61345790705681, 5.014806567272018, 4.618044173779868, 5.65158462801022, 5.548525993892823, 5.646393201598026, 6.159491988943552, 4.789419901289577, 4.6736512810108275, 4.363105511327151, 4.739391667120165, 4.897441960035855, 5.461516912637022, 5.243873127366143, 5.125065202894813, 4.351851614609344, 5.3655333223312445, 4.243655718883197, 3.7754875890613078, 4.719243395600352, 3.8813961449106613, 4.218969588152745, 4.257677718939865, 6.103674486703634, 7.791663380655756, 10.464238426233472, 8.594965185662373, 6.75186715111778, 6.031324685806287, 6.145523302453469, 6.75140945728926, 8.154059132392755, 4.106360394477029, 4.709620377998221, 6.028553163902719, 4.097065935296995, 5.0426994603803985, 3.2005053600326656, 4.092894804323027, 3.842959298407252, 5.233649653441752, 5.620989567976742, 6.240709912225148, 5.402477404353037, 4.949452795156887, 6.66384694626282, 6.878195322360906, 5.223144951942625, 4.420158019516105, 5.5748343085937835, 5.351436065486316, 2.803141976997137, 2.853987131694524, 3.628310473460109, 3.2999520776521822, 3.072156740561856, 4.022257331264973, 4.246632301136313, 3.2032110524464987, 3.890465629743822, 5.516141415478659, 4.606479518381352, 4.617117217288059, 5.2150682654367415, 4.3625251186388665, 4.610699586240319, 4.339292661652301, 5.338621562017436, 6.245388897091721, 5.252203657075473, 4.663563296139489, 5.613802045800598, 4.457795237559685, 4.511612941820016, 7.5219824401326605, 9.17922567246037, 6.09564157037004, 5.575136863116195, 8.148782813497581, 10.337371026863684, 11.397323448389315, 13.452285184002244, 19.5321880505188, 9.104925253521232, 5.54880509500184, 5.297901295579736, 5.988140824070068, 5.712105550155605, 6.082955341897327, 5.514548951533992, 5.464177314396885, 5.965901886583051, 5.349768122904307, 5.455309858192952, 5.298726867437543, 5.27469020516504, 4.649991348964344, 4.657421878443319, 5.590467970089882, 4.839346013516856, 4.563686472478843, 4.460033351195948, 4.796752890700532, 4.936679285281038, 4.9476732954772, 6.1392458938730945, 6.376675176853506, 7.1893167742280895, 7.830862993334671, 7.201136642310429, 8.819884427914946, 14.75778834627489, 10.720082995027086, 13.683835023100594, 9.083588248964963, 6.438601514845196, 5.770584825015614, 5.971641604445405, 5.947097347049473, 5.8707122742282944, 5.913629995689299, 8.46564700833827, 4.691928546549967, 4.93123094432014, 6.5383321864315365, 7.965339881360501, 9.553183457183184, 5.975672597974902, 7.285598168392091, 6.283135292771771, 5.087462116591565, 4.299544221293388, 4.452401951635002, 4.928524696311035, 4.3192558539968635, 5.270743879168626, 4.434409547593456, 6.315079614095595, 7.744781172450392, 8.08815578671949, 8.311096077776098, 7.919508946141913, 7.023719658943078, 4.2348297914384405, 5.297799056675912, 5.433120928375736, 6.883671910111391, 6.1792032956470955, 13.600344019383114, 6.080828732734664, 6.3592677654039225, 5.167408049888382, 5.908986243562096, 6.049967830296147, 5.389658284884827, 5.1795190101266915, 5.225140621772348, 4.885351697184914, 5.609925213863514, 6.063650915495623, 6.064491470070761, 5.417789742021097, 6.642258468903965, 4.4894790182232285, 5.328546012050812, 5.071869409181481, 4.934339573713094, 4.860577415921298, 6.361065045926173, 6.190369858616081, 7.752494922626216, 6.127335665675411, 5.952050620062956, 6.068822834066873, 4.265677036952063, 5.22676808371028, 4.947280614722526, 6.131532273754776, 5.640349902998796, 4.9605101666572144, 5.678809739479061, 4.719745254190277, 5.8758917407168365, 5.913293582954954, 5.485515502725772, 5.827580748165716, 5.104395243299189, 6.156313145512767, 5.704854390030226, 5.543713820717527, 6.963449964818909, 5.42907727323174, 4.9077764911701145, 5.223678986534636, 4.959746320310088, 5.887911278350437, 6.204625494405663, 4.813010715023378, 5.8089148090748335, 4.269984326680447, 4.503035786380268, 5.164840548106424, 4.593358661615181, 4.537538610033516, 4.23346846152204, 4.599166460471844, 4.658141927517007, 4.874112991746525, 6.166375609977758, 10.015845685546097, 6.486143999799107, 5.53486556416923, 5.270831174465323, 4.915667196745372, 6.0476493575518235, 6.313789492784983, 4.394310739262499, 4.966348552199283, 5.477472022486794, 4.400604850729746, 5.013639016806944, 5.014571233503277, 4.959505331690136, 4.5459152890121555, 4.760725343443551, 4.389542691319214, 4.5899672203500135, 4.871417868900642, 4.417547567530365, 5.431557397676423, 5.0063916174177425, 4.677939660871351, 4.912490579012375, 4.704522676025109, 4.676469616096843, 4.564527258579771, 4.31847476550115, 4.884535624222552, 4.361030312218816, 4.1987006350092715, 4.488650239277422, 4.964672710238733, 7.1043549698253825, 10.07783045950558, 13.138767501869758, 10.456322266224637, 9.166881106821982, 0]    

input_distancelist = [0.0, 4.087824158760445, 8.416829654805763, 16.489760803553693, 22.858918498786547, 31.78087190779509, 39.47777534665958, 45.85065511351118, 52.35898089281798, 60.66051304942781, 67.71288405240794, 76.62376361065739, 86.39354163829078, 94.75394515051694, 103.71362850181802, 111.27216946368478, 117.62903695453325, 126.60978434358435, 134.75453361297863, 142.64756781405455, 151.65912149772066, 160.13294247502517, 166.42323041679043, 172.53485313558602, 179.35764878011392, 187.0687168475811, 193.63401211900094, 201.09395970691278, 207.62433658857995, 214.85257257788373, 222.64650659949302, 230.7999142303188, 239.3444538302696, 245.9560359618332, 253.74916333195128, 261.3145094156179, 268.62769855391167, 276.1930679287745, 283.98609959125736, 291.0109300880504, 298.08674398155756, 305.26987015094574, 311.8453345533723, 319.2683216562706, 326.5577345689296, 333.1639699149783, 341.089471218815, 348.4873836536744, 356.2687847907752, 364.0862087481631, 371.0906705654512, 377.8767243299224, 384.97374723203956, 392.9825790972546, 400.3317454746838, 408.32330719262035, 416.3682358196568, 423.5517394977272, 431.1290259487073, 436.99150739467007, 444.3898920832804, 451.45121538083384, 458.3290088455969, 464.6199953216893, 471.717243091887, 479.2224791204489, 485.4670551924642, 492.899473685059, 498.6773694615957, 504.9435377107775, 511.4577951327448, 517.8403239499004, 524.7086403442719, 532.201072097167, 539.160198252126, 545.0361057412193, 551.05100874369, 556.2133513235237, 561.1250752389611, 567.5374179161081, 574.0830214638444, 582.1897811110265, 589.3999314164306, 596.1526231004325, 604.0593723030042, 610.7190703899448, 616.2490627617958, 623.7367225636532, 631.4022026751912, 638.4688470805338, 645.5017921955034, 654.2666368920637, 662.8719024445528, 670.0325210920518, 676.5995170139406, 682.6574223456819, 689.4545931050169, 696.0501422137825, 703.3270756023389, 710.2473387398869, 715.9158752256096, 723.1388311620137, 730.3335599320968, 737.4335223128868, 745.4406628704747, 753.4848443996515, 761.4023712427368, 768.8656084709943, 775.8003303884307, 783.2111257860241, 789.9637146843942, 796.9553893027283, 804.4167427532398, 811.4275532537163, 818.9436105890502, 827.7175519536598, 835.3695379459859, 842.1727313564011, 849.082470721635, 856.2753974365647, 863.0738690576441, 869.9890895904058, 877.326757016186, 883.9831856204079, 890.2778011321084, 897.6834184157178, 905.1102738984999, 914.2680484283256, 921.0638181390335, 927.302316351919, 934.8479796849883, 942.2144287602789, 949.3511150484037, 957.8083323937884, 964.9690733378777, 970.3975331880208, 975.1540953537805, 981.1654176166511, 987.4688404068477, 994.297071479403, 1001.8398376644643, 1008.8047725355505, 1015.5296470966207, 1024.2057870310082, 1031.476888600743, 1040.695756313634, 1049.7548815876291, 1058.4013618240147, 1065.045816941952, 1070.5670345300855, 1078.5022578305548, 1084.504244393815, 1091.4410434501328, 1097.363889067618, 1104.777834788606, 1114.7026737344358, 1122.2336822766545, 1130.2342882389257, 1137.5706554403384, 1145.8551414976243, 1153.300098998726, 1161.5171229075897, 1169.2900685094562, 1177.5887526280533, 1186.7689707557988, 1193.8800561650598, 1201.8158815728566, 1209.2825381202442, 1216.5115948004316, 1223.7439817619145, 1231.3627117780638, 1239.0753090944513, 1245.7668070459374, 1252.6630975006967, 1260.349969866839, 1268.2795249842825, 1277.0878534066046, 1284.6209891929236, 1292.7748472260105, 1299.176319211485, 1304.674385343202, 1313.5968196791698, 1322.4329185481022, 1329.1525425964517, 1334.5419544620527, 1340.5201016142348, 1348.925393107604, 1355.5198439662643, 1362.1133346747158, 1369.1533577863743, 1376.2941127199197, 1383.6716300490025, 1389.8997680853406, 1394.1369986122663, 1402.3199692935111, 1411.453188369646, 1419.4649369975978, 1427.3429998997478, 1435.4199746480365, 1441.379026298819, 1446.4623559663573, 1453.6419176497723, 1462.0694896716523, 1469.7712026290874, 1478.3811664073494, 1485.5996222986626, 1493.321077358964, 1500.5054465107498, 1509.2393114253803, 1517.4253531398674, 1525.5454919693434, 1531.6978017141046, 1537.2723890759723, 1544.5625911519437, 1551.1343786220061, 1558.3713897025916, 1565.7306470515364, 1571.843440755871, 1579.753241333986, 1586.3335277146512, 1593.5073537380388, 1602.3580312721115, 1609.3794878234246, 1617.7498549519107, 1626.0650280416266, 1633.4745650354876, 1641.1627414745603, 1647.2950612781008, 1652.8735871923507, 1658.1287188447377, 1665.2010003001624, 1670.5871972341272, 1677.092002563289, 1683.0236807287056, 1691.2556068129059, 1698.7388011408027, 1706.2636332263187, 1713.5521551456961, 1722.4042495110127, 1727.9290601363384, 1734.091502756006, 1741.4612953806952, 1750.085817503976, 1759.481599900724, 1768.203857307605, 1777.1931057352588, 1783.6674905889129, 1790.2096418276822, 1796.6361625455438, 1805.150300154924, 1812.714039791365, 1821.0969994472882, 1828.8751773910733, 1837.0972754948227, 1844.0765813688938, 1850.1144143903307, 1856.9335799716587, 1865.0147746550472, 1871.8674552321552, 1878.305801416041, 1885.163893808819, 1893.2250400661346, 1902.559675735365, 1911.568559309483, 1918.4743677968493, 1925.3067331812808, 1933.7958902986866, 1941.922644194629, 1947.9464033614597, 1953.8946476932801, 1959.8644593211461, 1967.9299727605141, 1975.6329863097415, 1982.434061492972, 1990.001335038096, 1997.657469032029, 2005.9788060255833, 2013.37490688066, 2020.168291106233, 2026.6739941936341, 2035.1699928345565, 2043.1275750542818, 2049.884817335686, 2056.936453999012, 2064.0388221842677, 2069.7046645697837, 2075.6510334004174, 2082.960070979789, 2088.8392127494944, 2095.1574417003185, 2103.922426835744, 2114.1411049557205, 2122.4691911282616, 2129.5341908490673, 2136.976886595009, 2144.5689600592764, 2152.509785848817, 2159.8390036452797, 2167.0934916685756, 2175.7570660148854, 2182.5184137892566, 2190.124347699705, 2199.228123945564, 2207.3546148404844, 2214.6553181745203, 2222.483173012267, 2227.8312424998508, 2233.258723685365, 2240.9560812616146, 2250.0562440472095, 2259.0755058888617, 2265.5838892469837, 2271.100537355006, 2278.114878820896, 2283.6903427482516, 2289.2422377243515, 2293.3870362348157, 2299.9202799850873, 2307.2988998544356, 2314.052667720879, 2319.9711786456464, 2325.1603765613345, 2330.711860159936, 2337.591344096401, 2344.185388037762, 2350.934498184642, 2358.055577675798, 2364.8300780164336, 2371.9549882740116, 2379.934358763901, 2387.5917749759683, 2395.433311308817, 2402.142368198882, 2409.704559245377, 2417.6106776445163, 2425.793923077375, 2435.0774128797752, 2444.1060048118557, 2450.724522973171, 2457.0096743702743, 2464.2628361695897, 2472.3337370292334, 2479.6149240342393, 2486.4568967226214, 2493.2447289830297, 2501.7765919731264, 2510.705750756328, 2519.2578250784086, 2525.002402930663, 2533.317941612467, 2540.907055675144, 2547.926355057581, 2556.1076955247445, 2562.4443973749953, 2566.0058086321906, 2568.5454708040347, 2574.740799048955, 2582.8099092836687, 2592.991151932846, 2601.475354844825, 2609.110601517834, 2616.843348343546, 2624.6132512447134, 2631.693446272542, 2636.946113251952, 2642.3824122725714, 2648.948250558034, 2655.7993786914226, 2663.8908693725684, 2671.973463197271, 2678.3370570262214, 2684.310849067762, 2690.346893738649, 2696.855182578897, 2702.5039816132944, 2709.3654946140514, 2715.7917585519035, 2722.6227428893917, 2730.7693869224604, 2737.659617921174, 2745.4881612585405, 2752.6108072590223, 2758.7761058905753, 2764.749866951605, 2771.9640971924473, 2778.3267460723937, 2785.538593083939, 2792.389816959504, 2798.6277319305195, 2805.7081180614086, 2811.744303599065, 2817.4820437620874, 2823.172722607368, 2829.328773574473, 2835.8862746543227, 2841.4926558949196, 2847.43703137571, 2855.500741613594, 2863.561225150303, 2869.9415581720737, 2876.317128169257, 2882.335538404113, 2888.455825374253, 2895.636105989244, 2902.5040136212247, 2909.7391074231195, 2915.2576130422026, 2921.684685512651, 2928.5578407078447, 2935.4253612396533, 2942.403737494589, 2949.026939355918, 2955.045162564108, 2961.2834287111946, 2968.995261981538, 2976.640038356202, 2983.8730927118513, 2990.8617676247154, 2998.9247918101223, 3005.7253888582904, 3012.195360830476, 3018.2695974368335, 3025.332360076029, 3032.176311728702, 3038.975060853193, 3046.188958899199, 3052.5557432898386, 3058.9661678605944, 3064.746537150112, 3069.1332012517587, 3073.80594056454, 3079.394235127384, 3085.348462230648, 3091.287948847506, 3098.131998704921, 3105.4855485963412, 3113.5378360228574, 3119.0684405159395, 3125.4333778634586, 3131.9660741846965, 3138.363636014497, 3147.184563537988, 3153.2935923216646, 3160.235199389139, 3167.0683321885026, 3172.1589103989104, 3177.9379193667737, 3184.5443466735055, 3191.605135477897, 3195.239306829935, 3201.5830035591785, 3208.7033536146864, 3214.2855964808155, 3223.3322135052867, 3230.275046158905, 3237.3968257513343, 3243.3964889615786, 3249.7116444071253, 3257.334617797681, 3265.4151628956215, 3272.1502904080617, 3278.5656150863106, 3285.8544308710375, 3289.390832075334, 3291.6847865234204, 3293.822487494344, 3298.6859561680035, 3303.4055189180303, 3308.2523502386266, 3315.794825388387, 3323.4298307012373, 3330.212873320064, 3335.872020022766, 3340.8498568433083, 3346.9597404787983, 3354.3804465152543, 3360.9870231360132, 3368.6206337585722, 3375.762723580857, 3382.3952570443907, 3388.508825403254, 3394.4123629337414, 3400.2856105388246, 3407.0522434502022, 3413.9202277651952, 3419.583032692493, 3424.432994150808, 3431.0184078052966, 3439.3093505230518, 3446.313044953006, 3453.492460107818, 3461.4895139304094, 3470.0954624165147, 3477.355508345067, 3484.341743916529, 3491.9273253568463, 3501.2311258098193, 3510.49861244688, 3519.6689402599072, 3527.6281952475583, 3534.875225636962, 3542.336041215329, 3548.586914997847, 3554.706955285324, 3560.878741562161, 3567.552512183708, 3573.864771352406, 3579.820869175551, 3585.903171113801, 3592.220482091751, 3598.985652030438, 3605.045484708306, 3610.259747095651, 3617.0717523812214, 3623.166031311483, 3629.2479442741783, 3634.986096052954, 3641.223216285674, 3645.0176578385913, 3650.92519192072, 3657.335139338436, 3664.2604119014704, 3672.1888547424273, 3679.438750454652, 3685.4531264918533, 3691.914035539646, 3698.610134554531, 3705.0596734360192, 3710.73838709394, 3717.7445680127535, 3725.055359490577, 3732.2259533402016, 3739.8692665352523, 3747.367964317488, 3754.4067618444265, 3760.099248195437, 3765.065915449015, 3770.3756051117743, 3774.5061440756945, 3779.615812774648, 3785.843901121496, 3793.219553124775, 3800.3560097067625, 3808.0765435898556, 3813.7225874933124, 3820.186587552854, 3825.7678320539017, 3832.6218693998303, 3839.652052828662, 3846.301023638688, 3855.1748150073818, 3864.2888700449107, 3872.7185371112887, 3879.596689107751, 3886.7979472281318, 3893.9901897921877, 3898.9925071625366, 3905.445796309138, 3913.661756340914, 3921.1661547135104, 3928.0428565669745, 3933.132673203049, 3939.174758684047, 3945.775378781245, 3950.2146734630337, 3956.516023277533, 3961.106331478863, 3966.4200135150513, 3972.0779150265334, 3977.8700044675747, 3983.8104226343803, 3992.1973371875956, 4000.818364612231, 4009.5560968023997, 4017.2510881136586, 4025.1200337042214, 4033.6824340032163, 4041.482047366937, 4047.989464857154, 4054.175839164697, 4059.579472199971, 4064.0258350748263, 4070.249403969677, 4077.7280796660507, 4085.102975073431, 4092.105510773697, 4098.398640110848, 4104.797586518191, 4109.713623561749, 4115.525199690537, 4122.347888841422, 4130.10746676292, 4136.210369597522, 4144.081923929813, 4152.202659923969, 4158.807007107478, 4166.949967743588, 4176.3783242371255, 4184.660679010621, 4191.925775431977, 4198.524606505984, 4204.877016082835, 4211.991924655188, 4218.66914149775, 4225.894379920831, 4232.541362787702, 4239.759425690303, 4245.6574765074365, 4251.665078052428, 4257.56855167589, 4262.980253386546, 4269.940038500471, 4277.0722208840325, 4284.712039184735, 4291.745290638626, 4298.551564935563, 4304.654875401599, 4311.011500080289, 4317.51548237392, 4325.175057259569, 4331.387549351381, 4339.242412125712, 4348.071293838873, 4355.134572957812, 4363.722548028855, 4371.6233713547535, 4379.452365358939, 4384.913556548925, 4389.191633219446, 4392.377085612214, 4396.255325094666, 4401.192231077453, 4406.718932933146, 4412.142935364318, 4417.080176031501, 4421.168119568838, 4429.285608082064, 4436.363319374688, 4441.892562034921, 4450.028465596091, 4456.638681730372, 4467.053703604603, 4475.197898588096, 4483.8717696195035, 4490.240811404983, 4496.17096532721, 4501.512238003262, 4507.682246841518, 4514.41699807825, 4519.419113763399, 4524.265346208263, 4530.647197303729, 4538.188405827532, 4544.167656777098, 4550.396514400627, 4562.287932533161, 4573.967499326983, 4583.154511733213, 4593.255668522451, 4604.105809367556, 4612.3930298480145, 4620.242386931371, 4630.648611431588, 4639.216566145926, 4645.259437570468, 4652.495621558766, 4659.715133597257, 4666.106868400471, 4673.74770310671, 4680.977263981335, 4688.659007512467, 4694.902816508124, 4700.240087554424, 4706.586629979226, 4713.734240339941, 4719.67198681931, 4727.149524830137, 4734.537865488608, 4738.9693213712335, 4742.600709739137, 4748.069097758433, 4754.048024222796, 4758.138614694556, 4761.363161172254, 4764.287824448178, 4766.765718194661, 4768.472302897158, 4772.1333250807775, 4778.140624447511, 4784.432424062794, 4789.998982083498, 4795.834541850496, 4801.314334395371, 4807.358950850187, 4813.459289732951, 4819.046598067387, 4825.277397714688, 4831.387652530603, 4837.678471847632, 4843.99795830784, 4851.166430425698, 4858.323465842141, 4864.2859959043235, 4871.17397895828, 4878.478015625643, 4885.951801292379, 4892.9009467023825, 4899.6531239063415, 4906.39029739025, 4911.819845890108, 4917.047230445359, 4921.6837396408955, 4925.940401181482, 4930.56930006079, 4934.3486390765975, 4936.607333438598, 4939.716762101491, 4942.152726381758, 4945.822348167744, 4950.999455845006, 4956.775877879188, 4962.357815902481, 4967.962791134621, 4973.640693879005, 4979.2773896745875, 4983.214871747783, 4990.319270930896, 4997.078908374307, 5002.177048447781, 5006.361845811085, 5009.851084082571, 5015.429256712817, 5020.004493030124, 5025.309700029515, 5031.861755370551, 5039.614515109188, 5047.101110815375, 5053.864459968983, 5061.581838716641, 5067.906056720303, 5075.423028929472, 5080.70139996678, 5085.005373518919, 5089.126626152068, 5093.137328593887, 5097.346343768947, 5102.092167200395, 5109.963400514358, 5116.255321551138, 5122.390530719636, 5127.232907545349, 5132.627346235659, 5135.078264630505, 5140.559973588018, 5145.801667217381, 5152.25235448874, 5157.893480048924, 5163.403151218812, 5169.587835213865, 5176.023439228092, 5182.402852864241, 5189.225971321105, 5195.167821178739, 5200.665059360467, 5206.161535610428, 5212.314106108835, 5217.332479526459, 5224.757246007833, 5231.012861190276, 5237.585059850004, 5244.3404387280425, 5251.1983345048175, 5256.438547125294, 5261.823255001341, 5266.122946089954, 5271.563048469208, 5277.163359263101, 5282.655912637079, 5290.470225123603, 5296.8476523978725, 5303.585360631793, 5309.02173963872, 5314.931538482158, 5321.651277423001, 5327.521052052287, 5334.5835801206695, 5340.256477921415, 5345.893494393828, 5351.970103477654, 5357.690029940076, 5364.220349741838, 5369.634845812043, 5375.477822872349, 5381.490639264271, 5386.277538502685, 5392.417317263559, 5399.20925927504, 5405.590457932012, 5412.311231774181, 5417.972548964183, 5423.344885032394, 5430.27055698847, 5436.008863441382, 5443.815293344428, 5451.217706941529, 5457.671600923901, 5464.928454957941, 5472.2745816133975, 5480.148346030518, 5487.396036154331, 5494.551965246234, 5501.390816415622, 5506.796476959458, 5510.124536753893, 5515.263696990602, 5521.286125710302, 5527.610238972517, 5534.391278452856, 5539.903061852088, 5545.182511439602, 5552.768077256673, 5559.479916545232, 5565.565448921571, 5573.140165230809, 5579.78869601286, 5586.435990822559, 5593.1570912356065, 5600.489681311339, 5607.4914154859935, 5615.085220963556, 5622.347436951842, 5629.1900717309545, 5636.735736565067, 5642.8727118177985, 5649.530867208073, 5656.6565113484085, 5663.441935726776, 5670.527316248631, 5677.655200329947, 5684.957891593467, 5692.676666194079, 5699.5009246083355, 5707.144378321542, 5715.083342356128, 5722.509479737396, 5729.223584629867, 5733.915542376195, 5737.223132582963, 5739.760153907725, 5742.948017908532, 5746.584296479717, 5746.584296479717]

average_distance = 1000

Running the function leads to a resulting average pace of

average_pace = average_over_distance(input_datalist, input_distancelist, average_distance)
average_pace = [4.715677705829617, 4.596201931814222, 4.924473000260758, 5.385001220009992, 5.695676885416609, 5.730593560295301]
# import numpy
import numpy as np

# defined a function to find the nearest value to a given one    
def find_nearest(array,value):
    idx = np.searchsorted(array, value, side="left")
    if idx > 0 and (idx == len(array) or math.fabs(value - array[idx-1]) < math.fabs(value - array[idx])):
        return array[idx-1]
    else:
        return array[idx]


# defined a function to calculate the average between two points of a list    
def mean(numbers):
    return float(sum(numbers)) / max(len(numbers), 1)


# defined main function    
def average_over_distance(input_datalist, input_distancelist, average_distance):

    output_average_list = []
    ratio_steps = int(input_distancelist[-1]/average_distance)

    distance_steps = range(average_distance,(ratio_steps+2)*average_distance,average_distance)

    for index1, elements1 in enumerate(input_distancelist):
        for index2, elements2 in enumerate(distance_steps):
            if elements1 == find_nearest(input_distancelist,elements2):
                if index2 == 0:
                    cutoff_index_start = 0
                    cutoff_index_end = index1
                else:
                    cutoff_index_start = cutoff_index_end+1
                    cutoff_index_end = index1

                output_average_list.append(mean(input_datalist[cutoff_index_start:cutoff_index_end]))
            # TODO: The last file will always be zero... Reason unclear... going from "distance_steps = range(average_distance,(ratio_steps+1)*average_distance,average_distance)" to "distance_steps = range(average_distance,(ratio_steps+2)*average_distance,average_distance) does not help
    del output_average_list[-1]

    return output_average_list
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to code review, I suggest you write a sample input and output to make it easier for people trying to review your code. ex: Input: [1, 2, 3] Output(expected): [4, 5, 6] \$\endgroup\$ – bullseye Dec 29 '19 at 9:12
  • 1
    \$\begingroup\$ @bullseye: Thanks for the remark, I added an input and output example \$\endgroup\$ – JoelAffolter1997 Dec 29 '19 at 12:16
1
\$\begingroup\$

There are some logical errors with this function as well as inefficient techniques.

  1. cutoff_index_start should not be cutoff_index_end + 1-- rather it should be just cutoff_index_end. This is because subarray slicing does not include the elements at the upper bound endpoint, meaning your original code is skipping elements occurring at index1s.
  2. The double for loop is unnecessary since you know the index at which your intervals are occurring. No need to check against every element in input_distancelist.
  3. np.searchsorted can take an array of values to insert.

With all those points, the code simply becomes:

def average_over_distance(input_datalist, input_distancelist, average_distance):

    output_average_list = []
    ratio_steps = int(input_distancelist[-1] / average_distance)

    distance_steps = range(0, (ratio_steps + 2) * average_distance, average_distance)

    breaks = np.searchsorted(input_distancelist, distance_steps)

    for index2, elements2 in enumerate(distance_steps[1:]):
        cutoff_index_start = breaks[index2]
        cutoff_index_end = breaks[index2 + 1]

        output_average_list.append(np.mean(input_datalist[cutoff_index_start:cutoff_index_end]))

    return output_average_list

You will notice slightly different results, this is due to numpy's float representation and the logical error I pointed out in #1 above.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your remarks ! \$\endgroup\$ – JoelAffolter1997 Jan 2 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.