0
>>>list = []
>>>stringsandnumbers = input('Enter in the values. For example, "A=3,B=2,C=1,D=5"... ').split(',')
>>>list.append(stringsandnumbers)
>>>list.sort()
>>>print(list)

[[A=3,B=2,C=1,D=5]]

The problem with this is that it is not sorting the list from min to max. I'm not sure if splitting/separating it would be a good idea or if there's a way to just sort the numbers from the list? Any help would be appreciated.

1
  • 2
    You appear to be creating a list with a single string in it - how do you expect to sort that?! You will need to parse the input into some usable format. Commented Sep 14, 2015 at 16:41

1 Answer 1

2

Think you mean this,

>>> s = "A=3,B=2,C=1,D=5"
>>> sorted(s.split(','), key = lambda m: int(m.split('=')[1]))
['C=1', 'B=2', 'A=3', 'D=5']
>>> ','.join(sorted(s.split(','), key = lambda m: int(m.split('=')[1])))
'C=1,B=2,A=3,D=5'
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I get: AttributeError: 'list' object has no attribute 'split'
you could apply the split function only on string not in list.

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.