Can I sort text by its numeric value in Python?

后端 未结 6 1938
轮回少年
轮回少年 2020-12-16 01:26

I have dict in Python with keys of the following form:

mydict = {\'0\'     : 10,
          \'1\'     : 23,
          \'2.0\'   : 321,
          \'2.1\'   : 3         


        
相关标签:
6条回答
  • 2020-12-16 01:48

    I would do a search on "sorting a python dictionary" and take a look at the answers. I would give PEP-265 a read as well. The sorted() function is what you are looking for.

    0 讨论(0)
  • 2020-12-16 01:49

    There is a nice sorting HOWTO on the python web site: http://wiki.python.org/moin/HowTo/Sorting . It makes a good introduction to sorting, and discusses different techniques to adapt the sorting result to your needs.

    0 讨论(0)
  • 2020-12-16 01:56

    You can sort the keys the way that you want, by splitting them on '.' and then converting each of the components into an integer, like this:

    sorted(mydict.keys(), key=lambda a:map(int,a.split('.')))
    

    which returns this:

    ['0',
     '1',
     '2.0',
     '2.1',
     '3',
     '4.0.0',
     '4.0.1',
     '5',
     '10',
     '11.0',
     '11.1',
     '12.0',
     '12.1.0',
     '12.1.1']
    

    You can iterate over that list of keys, and pull the values out of your dictionary as needed.

    You could also sort the result of mydict.items(), very similarly:

    sorted(mydict.items(), key=lambda a:map(int,a[0].split('.')))
    

    This gives you a sorted list of (key, value) pairs, like this:

    [('0', 10),
     ('1', 23),
     ('2.0', 321),
     ('2.1', 3231),
     ('3', 3),
     # ...
     ('12.1.1', 2)]
    
    0 讨论(0)
  • 2020-12-16 02:02

    As an addendum to Ian Clelland's answer, the map() call can be replaced with a list comprehension... if you prefer that style. It may also be more efficient (though negligibly in this case I suspect).

    sorted(mydict.keys(), key=lambda a: [int(i) for i in a.split('.')])

    0 讨论(0)
  • 2020-12-16 02:02

    For fun & usefulness (for googling ppl, mostly):

    f = lambda i: [int(j) if re.match(r"[0-9]+", j) else j for j in re.findall(r"([0-9]+|[^0-9]+)", i)]
    cmpg = lambda x, y: cmp(f(x), f(y))
    

    use as sorted(list, cmp=cmpg). Additionally, regexes might be pre-compiled (rarely necessary though, actually, with re module's caching). And, it may be (easily) modified, for example, to include negative values (add -? to num regex, probably) and/or to use float values.

    It might be not very efficient, but even with that it's quite useful.

    And, uhm, it can be used as key= for sorted() too.

    0 讨论(0)
  • 2020-12-16 02:14

    Python's sorting functions can take a custom compare function, so you just need to define a function that compares keys the way you like:

    def version_cmp(a, b):
      '''These keys just look like version numbers to me....'''
      ai = map(int, a.split('.'))
      bi = map(int, b.split('.'))
      return cmp(ai, bi)
    
    for k in sorted(mydict.keys(), version_cmp):
      print k, mydict[k]
    

    In this case you should better to use the key parameter to sorted(), though. See Ian Clelland's answer for an example for that.

    0 讨论(0)
提交回复
热议问题