2

I tried running the accepted answer from Does Python have a built in function for string natural sort?, but it gave me the following error:

File " < stdin >", line 4

convert = lambda text: int(text) if text.isdigit() else text 


SyntaxError: invalid syntax

Can anyone help please?

Thank you...


Update: Thank you everyone for your responses. I first integrated the code in my own script but received the error. Then I just tested this def on Python interactive window (just this line), and it gives same error.

I am using python/2.6.5. The strange thing is that this code runs on my other computer (Python 2.6.1). Is this code version-specific?

Community
  • 1
  • 1
fgar
  • 357
  • 1
  • 3
  • 9
  • 5
    It works for me: http://ideone.com/T76PlY What version of Python are you using? – Mark Amery Jan 27 '13 at 21:28
  • Can you post the entire error, and the Python version you're using (should be displayed when you start you interpreter)? – phihag Jan 27 '13 at 21:28
  • @root It's presumably copied and pasted from the answer he linked to. The line numbers match. – Mark Amery Jan 27 '13 at 21:32
  • Thanks everyone, I have edited my question. I am using python/2.6.5... – fgar Jan 27 '13 at 21:36
  • Be sure you're not mixing tabs and spaces. – Makoto Jan 27 '13 at 21:37
  • @Makoto If he were, in a simple case like this that could only possibly cause an `IndentationError`, not a `SyntaxError`. – Mark Amery Jan 27 '13 at 21:40
  • It's really the same script that I use on both computer, did not change anything. It works on one, but does not work on the other... – fgar Jan 27 '13 at 21:41
  • 1
    @fgar show the *full* traceback for the error. And, are you pretty sure you are not using an old python version on one of your computers? The `if` expression was added in 2.5 if I'm not mistaken. – Bakuriu Jan 27 '13 at 21:43
  • @Bakuriu He's pasting the code straight into the interpreter, and it's causing a `Syntax Error`; that means this ***is*** the full traceback. Including the little `^` arrow pointing to the location of the syntax error, if present, might help, though. – Mark Amery Jan 27 '13 at 21:44
  • Yes @Bakuriu, thanks, it's really my bad. I was using a machine with two Python installations on it, and when I thought I was using python/2.6.5, I was actually the other installation (2.4.3). Thanks for pointing out that the if expression was added in 2.5. – fgar Jan 27 '13 at 21:48
  • Thanks also @Mark Amery and everyone else for your help and responses! – fgar Jan 27 '13 at 21:48
  • Hi @Bakuriu, would you mind to post your comment as answer? I'll accept it. – fgar Jan 27 '13 at 21:52
  • @Bakuriu You're correct that it was added in 2.5. Here's a source: http://docs.python.org/2/faq/programming.html#is-there-an-equivalent-of-c-s-ternary-operator I suggest posting an answer, both for the record and to get some deserved rep. It was a nice question, especially given how most posters just assumed the asker was a moron when in fact there was a genuine problem. Well solved. :) – Mark Amery Jan 27 '13 at 21:53

1 Answers1

-1

The code below works in 2.6.x

import re

li = ['elm1', 'elm0', 'Elm18', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13']

def natural_sort(l): 
    convert = lambda text: int(text) if text.isdigit() else text.lower() 
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] 
    return sorted(l, key = alphanum_key)

print natural_sort(li)

Try it again, with copy & paste.

Excalibur
  • 286
  • 3
  • 11
  • 2
    Oh, I think I got it. This code works on 2.6.x and above, but not on Python 2.4.x (at least on 2.4.3). I was using a machine with two Python installations on it, and when I thought I was using python/2.6.5, I was actually the other installation (2.4.3). Thanks! – fgar Jan 27 '13 at 21:46
  • @fgar I'd post your own answer and accept it, in that case, or invite Bakuriu to do so. This answer doesn't really contain the solution. :) – Mark Amery Jan 27 '13 at 21:48
  • @Mark Amery, I think you're right. I'm sorry Excalibur, but thanks for your response – fgar Jan 27 '13 at 21:54