Finding k closest numbers to a given number

后端 未结 3 733
太阳男子
太阳男子 2020-12-23 20:56

Say I have a list [1,2,3,4,5,6,7]. I want to find the 3 closest numbers to, say, 6.5. Then the returned value would be [5,6,7].

Finding one

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 21:36

    Both answers were good, and Greg was right, Raymond's answer is more high level and easier to implement, but I built upon Greg's answer because it was easier to manipulate to fit my need.

    In case anyone is searching for a way to find the n closest values from a list of dicts.

    My dict looks like this, where npi is just an identifier that I need along with the value:

    mydict = {u'fnpi': u'1982650024',
     u'snpi': {u'npi': u'1932190360', u'value': 2672},
     u'snpis': [{u'npi': u'1831289255', u'value': 20},
      {u'npi': u'1831139799', u'value': 20},
      {u'npi': u'1386686137', u'value': 37},
      {u'npi': u'1457355257', u'value': 45},
      {u'npi': u'1427043645', u'value': 53},
      {u'npi': u'1477548675', u'value': 53},
      {u'npi': u'1851351514', u'value': 57},
      {u'npi': u'1366446171', u'value': 60},
      {u'npi': u'1568460640', u'value': 75},
      {u'npi': u'1326046673', u'value': 109},
      {u'npi': u'1548281124', u'value': 196},
      {u'npi': u'1912989989', u'value': 232},
      {u'npi': u'1336147685', u'value': 284},
      {u'npi': u'1801894142', u'value': 497},
      {u'npi': u'1538182779', u'value': 995},
      {u'npi': u'1932190360', u'value': 2672},
      {u'npi': u'1114020336', u'value': 3264}]}
    
    value = mydict['snpi']['value'] #value i'm working with below
    npi = mydict['snpi']['npi'] #npi (identifier) i'm working with below
    snpis = mydict['snpis'] #dict i'm working with below
    

    To get an [id, value] list (not just a list of values) , I use this:

    [[id,val] for diff, val, id in sorted((abs(x['value']-value), x['value'], x['npi']) for x in snpis)[:6]]
    

    Which produces this:

    [[u'1932190360', 2672],
     [u'1114020336', 3264],
     [u'1538182779', 995],
     [u'1801894142', 497],
     [u'1336147685', 284],
     [u'1912989989', 232]]
    

    EDIT

    I actually found it pretty easy to manipulate Raymond's answer too, if you're dealing with a dict (or list of lists).

    from heapq import nsmallest
    [[i['npi'], i['value']] for i in nsmallest(6, snpis, key=lambda x: abs(x['value']-value))]
    

    This will produce the same as the above output.

    And this

    nsmallest(6, snpis, key=lambda x: abs(x['value']-value)) will produce a dict instead.

提交回复
热议问题