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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import csv
import pandas as pd
path2data = '/home/pedro/myPython/csv/csv/satellites.csv'
moons = []
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)
Jupiter = [p for p in moons if p[ 0 ] = = 'Jupiter' ]
len (Jupiter)
|
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:
1 2 3 4 |
mylist = [i for i in 'qwerty' ]
newlist = sorted (mylist)
mylist
newlist
|
Using sort()
1 2 3 4 |
mylist = [i for i in 'qwerty' ]
mylist
mylist.sort()
mylist
|
Task 2:
1 2 3 4 5 6 7 8 9 |
task2 = "Print the moons of Jupiter in alphabetic order"
print (task2)
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!)