1

I'm new to python and have an array, where each element is an array of tuples. I need to sort the tuples in the inner array by the first element in the tuple. My array looks something like this:

[[(u'10:30', u'11:20'), (u'08:30', u'09:20')], [(u'14:30', u'15:50'), (u'10:00', u'11:20'), (u'10:30', u'11:20'), (u'13:00', u'14:20')], [(u'10:30', u'11:20'), (u'08:30', u'09:20')], [(u'14:30', u'15:50'), (u'10:00', u'11:20'), (u'13:00', u'14:20')], [(u'10:30', u'11:20'), (u'08:30', u'09:20')]]

I tried doing something like

for index, elm in array:
    array[index] = sorted(elm, key=lambda x: x[0])

But I get the ValueError: need more than 0 values to unpack. Not sure if my syntax is off or something

The expected result is

  [[ (u'08:30', u'09:20'), (u'10:30', u'11:20')], [ (u'10:00', u'11:20'), (u'10:30', u'11:20'), (u'13:00', u'14:20'), (u'14:30', u'15:50')], [(u'08:30', u'09:20'),(u'10:30', u'11:20') ], [ (u'10:00', u'11:20'), (u'13:00', u'14:20'), (u'14:30', u'15:50'),] [ (u'08:30', u'09:20'), (u'10:30', u'11:20')]]
0

2 Answers 2

4

for statement is missing enumerate:

for index, elm in enumerate(array):

But, if you want to sort the list in-place, you don't need to use index. Use list.sort instead:

for elm in array:                                                              
    elm.sort(key=lambda x: x[0])
Sign up to request clarification or add additional context in comments.

5 Comments

@The6thSense, Strings like 10:30 cannot be converted to int.
Sorry did not see the actual data now I have a little doubt should not it be sorted according to time than normal strings ? It provides the correct output but is string comparsion enough here ?
@The6thSense, Strings are sorted according to lexicographical order. '10:30' < '11:45'
@The6thSense: Normal (lexicographic) string sorting will sort these time strings correctly because they are zero-padded. If we had times like 2:30 then there would be problems because eg, '2:30' > '10:30' is True.
@PM2Ring hmm yeah that makes sense :).
1

When you do for index, elm in array:, you're iterating over pairs of objects. On the first loop iteration, index is (u'10:30', u'11:20') and elm is (u'08:30', u'09:20'), which isn't what you want (I get TypeError when I enter your posted code, not ValueError). You can get what you planned with the enumerate() function:

for index, elm in enumerate(array):
    array[index] = sorted(elm, key=lambda x: x[0])

2 Comments

@The6thSense - There's a leading zero, so that's not necessary. If there was no leading zero, a simple int() call wouldn't be enough to parse these time strings. Adding a leading zero with string formatting would be easier.
FWIW, using the sorted function when the result is going to replace the original list isn't very efficient. That's because sorted copies the source list to a new list and does an in-place sort of that new list. So in this situation it's much better to simply do an in-place sort directly, using the .sort() method.

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.