2

I'm trying to sort a numpy array containing classes from a variable.

groups =['(0, 10]', '(10, 20]', '(100, 110]', '(110, 120]', '(120, 130]',
         '(130, 140]', '(140, 150]', '(150, 160]', '(160, 170]', '(170, 180]', 
         '(180, 190]', '(190, 200]', '(20, 30]', '(200, 210]', '(210, 220]', 
         '(230, 240]', '(30, 40]', '(40, 50]', '(50, 60]', '(60, 70]', 
         '(70, 80]', '(80, 90]', '(90, 100]']

I want this:

groups = ['(0, 10]', '(10, 20]','(30, 40]','(40, 50]', '(50, 60]', ...]

I tried this:

sort(groups)

but nothing........

4
  • 1
    NameError: name 'sort' is not defined Commented Dec 30, 2016 at 14:09
  • Was this array generate using pandas.cut? Commented Dec 30, 2016 at 14:11
  • Yes i used pandas.cut Commented Dec 30, 2016 at 14:12
  • 1
    Did you convert the resulting categories to a list of strings, or are these elements still of categorical dtype in your actual code? Commented Dec 30, 2016 at 14:29

3 Answers 3

4

The sorting function is sorted. We'll use a key function to extract the numbers and convert them to a tuple of integers.

sorted(groups, key=lambda x: tuple(map(int, x[1:-1].split(','))))

Output:

['(0, 10]', '(10, 20]', '(20, 30]', '(30, 40]', '(40, 50]', '(50, 60]',
 '(60, 70]', '(70, 80]', '(80, 90]', '(90, 100]', '(100, 110]', '(110, 120]',
'(120, 130]', '(130, 140]', '(140, 150]', '(150, 160]', '(160, 170]', 
'(170, 180]', '(180, 190]', '(190, 200]', '(200, 210]', '(210, 220]', '(230, 240]']
Sign up to request clarification or add additional context in comments.

2 Comments

It doesnt work... I want to find a way without losing the formation of the classes
@dimitrisGmk what do you mean by the 'formation' of the classes? The key functions sorts them as if they were tuples but does not actually change the values. sorted creates a new list, and doesn't modify the old one.
2

You have to interpret your strings as tuples of numbers otherwise they will be sorted by lexicographical order (which would give the wrong result!):

def interpret_as_tuple(x):
    splitted = x.split(',')
    first = int(splitted[0].strip('( '))
    second = int(splitted[1].strip('] '))
    return first, second

groups.sort(key=interpret_as_tuple)

groups

returns:

['(0, 10]', '(10, 20]', '(20, 30]', '(30, 40]', '(40, 50]', '(50, 60]', '(60, 70]',
 '(70, 80]', '(80, 90]', '(90, 100]', '(100, 110]', '(110, 120]', '(120, 130]',
 '(130, 140]', '(140, 150]', '(150, 160]', '(160, 170]', '(170, 180]', '(180, 190]',
 '(190, 200]', '(200, 210]', '(210, 220]', '(230, 240]']

Comments

-3

There are two problems with your elements

  • they are strings, not tuples of numbers
  • the closing ] does not match the opening (

So, you first have to convert them

groups = [eval(group[:-1] + ')') for group in groups]
print(sorted(groups))

3 Comments

Avoid eval please.
I know, but can't think of a better way in this case
ast.literal_eval is much safer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.