Re: using split for a string : error

Discussion in 'Python' started by Oscar Benjamin, Jan 25, 2013.

  1. On 25 January 2013 01:11, Chris Angelico <> wrote:
    > On Fri, Jan 25, 2013 at 12:03 PM, Oscar Benjamin
    > <> wrote:
    >> On 24 January 2013 11:35, Chris Angelico <> wrote:
    >>>
    >>> It's usually fine to have int() complain about any non-numerics in the
    >>> string, but I must confess, I do sometimes yearn for atoi() semantics:
    >>> atoi("123asd") == 123, and atoi("qqq") == 0. I've not seen a
    >>> convenient Python function for doing that. Usually it involves
    >>> manually getting the digits off the front. All I want is to suppress
    >>> the error on finding a non-digit. Oh well.
    >>>

    >>
    >> I'm interested to know what the situations are where you want the
    >> behaviour of atoi().

    >
    > It simplifies operations on strings that contain numbers. For
    > instance, here's a problem from yesterday. I have a set of files (MIDI
    > files of The Rose of Persia) which have been sloppily numbered:
    >
    > Rose_1.mid
    > Rose_10.mid
    > Rose_11.mid
    > Rose_12.mid
    > Rose_13.mid
    > Rose_14.mid
    > Rose_15.mid
    > Rose_16.mid
    > Rose_17.mid
    > Rose_18.mid
    > Rose_19.mid
    > Rose_2.mid
    > Rose_20.mid
    > Rose_21.mid
    > Rose_22.mid
    > Rose_23.mid
    > Rose_24.mid
    > Rose_3.mid
    > Rose_4.mid
    > Rose_5.mid
    > Rose_6.mid
    > Rose_7.mid
    > Rose_8.mid
    > Rose_9.mid
    > Rose_Int.mid
    >
    > They're not in order. The one marked "Int" is the Introduction and
    > should be first; then Rose_1 ... Rose_9, then Rose_10 ... Rose_24. In
    > fact, the correct sort order is exactly:
    >
    > atoi(filename[5:])


    I have solved similar situations with
    sorted(filenames, key=lambda s: (len(s), s))
    which is better than lexicographical ordering for sorting integer
    strings. It gets the _Int file wrong in this case (but I consider it
    luck that atoi does what you want for that file).


    Oscar
    Oscar Benjamin, Jan 25, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    453
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    688
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    203
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    230
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    592
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page