117.81008911132812, 116.40049913194444, 132.6966349283854, 139.83181762695312, 129.08833482530383, 99.76490953233507, 132.69677734375, 117.81746080186632, 132.66025119357639, 100.42313300238715, 105.30181545681424, 155.56293063693576, 120.6885986328125, 107.1498514811198, 100.41515435112848, 105.30535888671875, 155.5640394422743, 132.65536159939236, 137.21171061197916, 150.28360324435764, 116.40386962890625, 172.6755608452691, 136.65747748480902, 139.99466281467014, 137.20905558268228, 144.14166937934027, 137.97456868489584, 144.14078097873264, 172.5401136610243, 138.22279188368054, 176.089111328125, 155.05335489908853, 145.78936089409723, 41.650783962673614, 152.47548082139758, 136.87349785698785, 130.15458509657117, 115.51886664496527, 111.95757378472223, 106.1067606608073, 135.65361870659723, 152.4756096733941, 142.81195068359375, 119.00228881835938, 139.1741197374132, 145.24830457899304, 106.11417304144965, 153.58861626519098, 120.68910725911458, 155.5628187391493, 126.3281012641059, 

Where are very similar numbers. For example, 0 and 7 elements of an array Is it possible to somehow find all such close numbers? You need the number of such pairs

    3 answers 3

    Sort the list of numbers, loop through the first number with the second, the second with the third, and so on. If the difference between two numbers is insignificant, a pair of close numbers is found:

     def is_small_equal(a: float, b: float) -> bool: return abs(a - b) <= 0.01 items = [117.81008911132812, 116.40049913194444, 132.6966349283854, 139.83181762695312, 129.08833482530383, 99.76490953233507, 132.69677734375, 117.81746080186632, 132.66025119357639, 100.42313300238715, 105.30181545681424, 155.56293063693576, 120.6885986328125, 107.1498514811198, 100.41515435112848, 105.30535888671875, 155.5640394422743, 132.65536159939236, 137.21171061197916, 150.28360324435764, 116.40386962890625, 172.6755608452691, 136.65747748480902, 139.99466281467014, 137.20905558268228, 144.14166937934027, 137.97456868489584, 144.14078097873264, 172.5401136610243, 138.22279188368054, 176.089111328125, 155.05335489908853, 145.78936089409723, 41.650783962673614, 152.47548082139758, 136.87349785698785, 130.15458509657117, 115.51886664496527, 111.95757378472223, 106.1067606608073, 135.65361870659723, 152.4756096733941, 142.81195068359375, 119.00228881835938, 139.1741197374132, 145.24830457899304, 106.11417304144965, 153.58861626519098, 120.68910725911458, 155.5628187391493, 126.3281012641059] items.sort() for i in range(len(items) - 1): a, b = items[i], items[i + 1] if is_small_equal(a, b): print(a, b) 

    Result:

     100.41515435112848 100.42313300238715 105.30181545681424 105.30535888671875 106.1067606608073 106.11417304144965 116.40049913194444 116.40386962890625 117.81008911132812 117.81746080186632 120.6885986328125 120.68910725911458 132.65536159939236 132.66025119357639 132.6966349283854 132.69677734375 137.20905558268228 137.21171061197916 144.14078097873264 144.14166937934027 152.47548082139758 152.4756096733941 155.5628187391493 155.56293063693576 155.56293063693576 155.5640394422743 
    • I was thinking of going through the list, but I was looking for a more optimized solution) - overfitt
    • Thanks for the reply - overfitt
    • one
      @strawdog in the task does not indicate that a number can have only one close number. In general, there, of course, it is not said at all what "close numbers" are. But if the threshold of “proximity” can be set with hands, then the first number can have both 2nd and 3rd ones close, etc. - suit
    • one
      @strawdog 1st number and 3rd number is not a pair?) or if in one pair the number fell, then the other can not? If so, then using this algorithm, the 2-number can be paired with the first and the third. - suit
    • one
      @strawdog is just this condition is not in the problem - suit
     def pairs(lst): lst.sort() for i in range(0, len(lst)-1): if abs(lst[i]-lst[i+1])<0.01: yield [lst[i], lst[i+1]] print(list(pairs(lst))) 

    at the exit:

     [[100.41515435112848, 100.42313300238715], [105.30181545681424, 105.30535888671875], [106.1067606608073, 106.11417304144965], [116.40049913194444, 116.40386962890625], [117.81008911132812, 117.81746080186632], [120.6885986328125, 120.68910725911458], [132.65536159939236, 132.66025119357639], [132.6966349283854, 132.69677734375], [137.20905558268228, 137.21171061197916], [144.14078097873264, 144.14166937934027], [152.47548082139758, 152.4756096733941], [155.5628187391493, 155.56293063693576], [155.56293063693576, 155.5640394422743]] 

    For clarity:

     lst1=[11.0003, 11.0001, 11.0002, 11.0004] def pairs(lst): lst.sort() for i in range(0, len(lst)-1): if abs(lst[i]-lst[i+1])<0.01: yield [lst[i], lst[i+1]] print(list(pairs(lst1))) [[11.0001, 11.0002], [11.0002, 11.0003], [11.0003, 11.0004]] 
       items = [117.81008911132812, 116.40049913194444, 132.6966349283854, 139.83181762695312, 129.08833482530383, 99.76490953233507, 132.69677734375, 117.81746080186632, 132.66025119357639, 100.42313300238715, 105.30181545681424, 155.56293063693576, 120.6885986328125, 107.1498514811198, 100.41515435112848, 105.30535888671875, 155.5640394422743, 132.65536159939236, 137.21171061197916, 150.28360324435764, 116.40386962890625, 172.6755608452691, 136.65747748480902, 139.99466281467014, 137.20905558268228, 144.14166937934027, 137.97456868489584, 144.14078097873264, 172.5401136610243, 138.22279188368054, 176.089111328125, 155.05335489908853, 145.78936089409723, 41.650783962673614, 152.47548082139758, 136.87349785698785, 130.15458509657117, 115.51886664496527, 111.95757378472223, 106.1067606608073, 135.65361870659723, 152.4756096733941, 142.81195068359375, 119.00228881835938, 139.1741197374132, 145.24830457899304, 106.11417304144965, 153.58861626519098, 120.68910725911458, 155.5628187391493, 126.3281012641059] def k_closest(sample, pivot, k): return sorted(sample, key=lambda i: abs(i - pivot))[:k] rez = [ k_closest(items, i, 2) for i in items] from pprint import pprint as pp pp(rez) [[117.81008911132812, 117.81746080186632], [116.40049913194444, 116.40386962890625], [132.6966349283854, 132.69677734375], [139.83181762695312, 139.99466281467014], [129.08833482530383, 130.15458509657117], [99.76490953233507, 100.41515435112848], [132.69677734375, 132.6966349283854], [117.81746080186632, 117.81008911132812], ... [106.11417304144965, 106.1067606608073], [153.58861626519098, 152.4756096733941], [120.68910725911458, 120.6885986328125], [155.5628187391493, 155.56293063693576], [126.3281012641059, 129.08833482530383]]