Do not forget to put commas between the elements of a list
myVar = [['blueberries','fruit','5.20'],
['bean sprouts','vegetable','9.25'],
['tulip','flower','8.93']]
Now you could use the sorted builtin as Andy Knight suggested in the comments and specify a key function to use for comparing items.
Sort list by price and get a copy of the sorted list
sorted(myVar, key=lambda x: float(x[2]))
Sort list by item name
sorted(myVar, key=lambda x: x[0])
Answer from Dima Chubarov on Stack OverflowHow would you sort a 2D list by row based on the first column using a separate list as a key
Sorting 2D List in Python - Stack Overflow
Sorting 2d Array
python - sort a 2D list first by 1st column and then by 2nd column - Stack Overflow
so i have a data set of rain data over the course of a year where it ends up being formatted into something like this except its a lot longer
[['Nov', 0.0], ['Nov', 0.0], ['Nov', 0.09],['Feb', 0.0], ['Feb', 0.0], ['Feb', 0.09],['Oct', 0.56], ['Oct', 0.0], ['Oct', 0.03], ['Oct', 0.62]]
now I figured I would need another list to function as a lookup to help sort this data by month according to calendar order so I made one
month_lookup = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
but now I am a tad confused about how I would go about using something like the sorted() function to use the lookup as the way to sort the 2D list above
Do not forget to put commas between the elements of a list
myVar = [['blueberries','fruit','5.20'],
['bean sprouts','vegetable','9.25'],
['tulip','flower','8.93']]
Now you could use the sorted builtin as Andy Knight suggested in the comments and specify a key function to use for comparing items.
Sort list by price and get a copy of the sorted list
sorted(myVar, key=lambda x: float(x[2]))
Sort list by item name
sorted(myVar, key=lambda x: x[0])
Consider using something like this:-
myList = [['blueberries', 'fruit', 5.20], [
'bean sprouts', 'vegetable', 9.25], ['tulip', 'flower', 8.93]]
for i in range(3):
print(sorted(myList, key=lambda x: x[i]))
Based on the comments, looks like I have to do this when appending:
mylist.append([file, value])
And for sorting, I have to do this:
mylist.sort(key=lambda mylist: mylist[1])
I don't understand what this message means.
delete the files with lower half of values
Does this mean that you have to select the files having value less than the midpoint between minimum and maximum values on the files or that you just have to select the lower half of the files?
There isn't any need to use a 2D-array if the second coordinate depends on the first thanks to my_function. Here is a function that does what you need:
from os import listdir as ls
from os import remove as rm
from os.path import realpath
def delete_low_score_files(dir, func, criterion="midpoint")
"""Delete files having low score according to function f
Args:
dir (str): path of the dir;
func (fun): function that score the files;
criterion (str): can be "midpoint" or "half-list";
Returns:
(list) deleted files.
"""
files = ls(dir)
sorted_files = sorted(files, key=func)
if criterion == "midpoint":
midpoint = func(sorted_files[-1]) - func(sorted_files[0])
files_to_delete = [f for f in sorted_files if func(f) < midpoint]
if criterion == "half-list":
n = len(sorted_files)/2
files_to_delete = sorted_files[:n]
for f in files_to_delete:
rm(realpath(f))
return files_to_delete
Python's sort function is stable, which means that if two elements compare equal to each other, their relative positions don't change. Elements only switch positions if they compare unequal to each other.
Additionally, sorting takes an optional key parameter, which is a function that determines which values the sort should compare. You can define a full function for this, but it's common to define a short lambda function if you're only going to use it once.
Put these together, and you can first sort the lists by their second elements, then sort them a second time by their first elements. The relative positions from the first sort will be maintained after the second.
a = [
['abc', 5],
['abd', 21],
['abb', 10],
['abc', 3],
['abb', 15],
['abd', 20]
]
# Sort by second element first, in descending order
a = sorted(a, key=lambda x: x[1], reverse=True)
# Then sort by first element, in ascending order
a = sorted(a, key=lambda x: x[0])
print(a) # [['abb', 15], ['abb', 10], ['abc', 5], ['abc', 3], ['abd', 21], ['abd', 20]]
The method is described in nauseating detail here: https://portingguide.readthedocs.io/en/latest/comparisons.html