Let's review how the operator.itemgetter() works -
Say you have got a list of tuple like this -
list1 = [(1,2,3),
(4,5,6)]
If I select operator.itemgetter(0). That means I want the 1st value from the tuple. This function can be mapped to a list via -
#map
print(list(map(operator.itemgetter(0), list1))) #
#list comprehension
print([operator.itemgetter(1)(val) for val in list1])
The 1st one will print - # [1,4]
The 2nd one will print - # [2,5]
Some suggestion on file reading -
Use context manager to open the file. It'll automatically close the file after reading.
The lines from the file will contain the '\n'(A newline character). That you may wanna strip off.
with open('Mileage.txt', 'r') as car:
car_content = car.read().splitlines()
When you read the file content like this. List car_content will contain the list of strings -
['Prius,2.1', 'Camry,4.1', 'Sebring,4.2', 'Mustang,5.3 ', 'Accord,4.1', 'Camry,3.8', 'Camry,3.9', 'Mustang,5.2', 'Accord,4.3', 'Prius,2.3', 'Camry,4.2', 'Accord,4.4']
operator.itemgetter(1) will not work on the above list as every item in the list contains 1 single string separated via ',' and that's why you're getting the error list index out of range.
Now, what you need to do is to split this list on ',' -
car_content = [tuple(car.split(',')) for car in car_content]
This will give you the list of tuples -
[('Prius', '2.1'),
('Camry', '4.1'),
('Sebring', '4.2'),
('Mustang', '5.3 '),
('Accord', '4.1'),
('Camry', '3.8'),
('Camry', '3.9'),
('Mustang', '5.2'),
('Accord', '4.3'),
('Prius', '2.3'),
('Camry', '4.2'),
('Accord', '4.4')]
You can use the sorted function now with either 0 or 1.
Here's the complete code-
import operator
with open('test.txt', 'r') as car:
car_content = car.read().splitlines()
car_content = [tuple(car.split(',')) for car in car_content]
sorted_content = sorted(car_content, key = operator.itemgetter(1), reverse=True)
print(sorted_content)
With output -
[('Mustang', '5.3 '),
('Mustang', '5.2'),
('Accord', '4.4'),
('Accord', '4.3'),
('Sebring', '4.2'),
('Camry', '4.2'),
('Camry', '4.1'),
('Accord', '4.1'),
('Camry', '3.9'),
('Camry', '3.8'),
('Prius', '2.3'),
('Prius', '2.1')]