How do I do this

test_data = [['1.5.34'],['0.10.8'],['6.15.43'],['1.4.55'],['1.5.53']]

I want to sort by the third data point (34, 8, 43, 55, 53) rather than the whole set. I want the whole set to be sorted by the third data point. Output would be:
[‘0.10.8’].[‘1,5,34’],[‘6.15.43’],[‘1.5.53’],[‘1.4.55’]

This is a normal sort.

test_data = [['1,5,34'],['0.10.8'],['6.15.43'],['1.4.55'],['1.5.53']]
    test_data.sort()
    print(test_data)

Output:

[['0.10.8'], ['1,5,34'], ['1.4.55'], ['1.5.53'], ['6.15.43']]

How is the best way to do this?

Thanks

The sort method takes a key parameter, which is expected to be a function that takes a single item and should return some transformation of that item that can be used as a sort key. So in your case, you’d want to create a function that extracts the single item from the list, splits off the last bit that you’re interested in, and turns it into an int. Something like lambda i: int(i[0].split('.')[-1].split(',')[-1]) would probably do it, but that’s cryptic enough that you’d probably want to rewrite it as a named function.

1 Like

A list of lists, each containing a single string? Is that intentional?

I also see that the first string has commas whereas the others have full stops (periods). Is that intentional?

Anyway, sssuming that you want them to be sorted by the numeric value of the last number:

def make_key(item):
    return int(item[0].replace('.', ',').split(',')[-1])

test_data.sort(key=make_key)

The best way to do this is to convert the data into something easier to work with.

You have:

  • a list of lists (this is fine);
  • each inner list only has a single item (this is unusual);
  • each item is a string (but you want to treat them as numbers);
  • the strings are made up of three numeric substrings;
  • separated by dots;
  • except for the first, which uses commas.

Let’s break the strings up into lists of numbers:

test_data = [['1.5.34'], ['0.10.8'], ['6.15.43'], ['1.4.55'], ['1.5.53']]
data = []  # Accumulate our processed data.
for sublist in test_data:
    item = sublist[0]  # Extract the first (and only) string from the sublist.
    item = item.replace(',', '.')  # Convert commas to dots.
    values = item.split('.')  # Split on the dots.
    numbers = [int(s) for s in values]  # Convert to integers.
    data.append(numbers)  # And store them for further use.

Here’s a shorter way to do the same, without all the temporary variables:

test_data = [['1,5,34'], ['0.10.8'], ['6.15.43'], ['1.4.55'], ['1.5.53']]
data = [[int(s) for s in item[0].replace(',', '.').split('.')] for item in test_data]

However you do it, now we can easily sort the list-of-lists by the last number:

data.sort(key=lambda a: a[-1])

If you have more processing to do, now is probably the best time to do it, while the data is in an easy format to work with.

And finally, if you really must (but why?) we can reassemble into a list of lists containing a single string:

test_data = [['.'.join(map(str, sublist))] for sublist in data]

Putting it all together as a three step process:

test_data = [['1,5,34'], ['0.10.8'], ['6.15.43'], ['1.4.55'], ['1.5.53']]
data = [[int(s) for s in item[0].replace(',', '.').split('.')] for item in test_data]
data.sort(key=lambda a: a[-1])
test_data = [['.'.join(map(str, sublist))] for sublist in data]

Yes, can be done, but, there has to be a better way, I think. This is what I’m doing. I’m working on an alarm clock project that will take many alarms for the weeks as well as long running repeat alarms (5 hour max). This is for medical attendants to remember to care for their charges, on time. Turn patients, Meds, baths, ect.
The front end is complete. Output when the alarm is ‘SET’ the state of the alarm. Hours, minutes, AM, PM, day of week, repeat, reason for the alarm, alarm sound, etc. Each alarm can cover many individual alarm outputs
Example:

test_data = [1, 0, 5, 30, 1, 1, 1, 1, 1, 0, 0, 1, 'This is a test', 'Default Alarm', 1, 1, 30, 20, 27, 21, 57]

Need to sort by day of the week; Must keep the list intact when sorting. We have seven empty lists to begin with. When an alarm is entered we sort by day of week. Alarms, have to keep them in latest to earliest (largest to smallest). Example “Sorting By: test_data[:-1]
Lets for fun say on Monday we have 11 alarms times for the day. Each will have different alarm sounds etc. We have to put them in latest to earliest order. Then append each day list from zero through six into one list. Then it is easy. Just current day and time pointers to place in list of day alarms.
Editing is very easy also doing it this way, using state of alarm or machine.

I hope I have explained this well enough. Thanks.
P.S. I’m looking at moving lists such as, delete last sub list, sub_list_up_one, ect.

I was hopeing there was someone who had to do this before. Well, I’ll have to try doing it myself, step by step.
Thanks for your time for reading about my next problem.