To sort by radius, I would first change the entries, some of which look like: 1821.6±0.5, 1560.8±0.5
These will give odd results! Change them into float numbers like 1821.6 or 1560.8, to make the sorting easy!
Without getting into pandas for now, as I am very bad in pandas, you can easily get the Jupiter data like this:
import csv
import pandas as pd
path2data = '/home/pedro/myPython/csv/csv/satellites.csv'
moons = []
# put all the data in moons for safe-keeping and further use
with open(path2data) as infile:
data = csv.reader(infile, delimiter=',')
for line in data:
moons.append(line)
task1 = "Print all the moons from Jupiter, and rank them from highest to lowest radius."
print(task1)
# just get Jupiter stuff as a list
Jupiter = [p for p in moons if p[0] == 'Jupiter']
len(Jupiter) # returns 67
Now you have a list with all the data on the moons of Jupiter! (Didn't know there were so many!)
For sorting lists, look up sort() and sorted(), for example
here.
.sort() and sorted() can be confusing.
The difference between sort() and sorted() is:
Quote:mylist.sort() returns None: sort() sorts the list you are using, for instance here, mylist and so changes it directly.
Quote:sorted(mylist) returns a new list, sorted the way you want.
Using sorted:
mylist = [i for i in 'qwerty']
newlist = sorted(mylist)
mylist # returns ['q', 'w', 'e', 'r', 't', 'y']
newlist # returns ['e', 'q', 'r', 't', 'w', 'y']
Using sort()
mylist = [i for i in 'qwerty']
mylist # returns ['q', 'w', 'e', 'r', 't', 'y']
mylist.sort()
mylist # returns ['e', 'q', 'r', 't', 'w', 'y']
Task 2:
task2 = "Print the moons of Jupiter in alphabetic order"
print(task2)
# Jupiter is a list of lists
# sorted loops through Jupiter and here looks at the second element x[1], the name of the moon, in each list
Jupiter_moons_by_alphabet = sorted(Jupiter, key=lambda x: x[1])
for j in Jupiter_moons_by_alphabet:
print(j[1])
print('Task 2 done!')
Apparently, people with very good eyesight can actually see the 4 main moons of Jupiter with the naked eye! (No chance there for me!)