Loop through the rows in legislators, and extract the gender column (fourth column)
Append the genders to genders_list.
Then turn genders_list into a set, and assign it to unique_genders
Finally, convert unique_genders back into a list, and assign it to unique_genders_list.
In [4]:
# We can use the set() function to convert lists into sets.
# A set is a data type, just like a list, but it only contains each value once.
car_makers = ["Ford", "Volvo", "Audi", "Ford", "Volvo"]
# Volvo and ford are duplicates
print(car_makers)
# Converting to a set
unique_car_makers = set(car_makers)
print(unique_car_makers)
# We can't index sets, so we need to convert back into a list first.
unique_cars_list = list(unique_car_makers)
print(unique_cars_list[0])
genders_list = []
unique_genders = set()
unique_genders_list = []
In [5]:
from legislators import legislators
In [6]:
genders_list = []
for leg in legislators:
genders_list.append(leg[3])
unique_genders = set(genders_list)
unique_gender_list = list(unique_genders)
In [8]:
print(unique_gender_list)
In [9]:
for leg in legislators:
if leg[3] == '':
leg[3] = 'M'
Loop through the rows in legislators
Inside the loop, get the birthday column from the row, and split the birthday.
After splitting the birthday, get the birth year, and append it to birth_years
At the end, birth_years will contain the birth years of all the congresspeople in the data.
In [11]:
birth_years = []
for row in legislators:
birth_year = row[2].split("-")[0]
birth_years.append(birth_year)
print(birth_years)
In [12]:
dogs = ["labrador", "poodle", "collie"]
cats = ["siamese", "persian", "somali"]
# Enumerate the dogs list, and print the values.
for i, dog in enumerate(dogs):
# Will print the dog at the current loop iteration.
print(dog)
# This will equal dog. Prints the dog at index i.
print(dogs[i])
# Print the cat at index i.
print(cats[i])
ships = ["Andrea Doria", "Titanic", "Lusitania"]
cars = ["Ford Edsel", "Ford Pinto", "Yugo"]
for i, e in enumerate(ships):
print(e)
print(cars[i])
In [14]:
lolists = [["apple", "monkey"], ["orange", "dog"], ["banana", "cat"]]
trees = ["cedar", "maple", "fig"]
for i, row in enumerate(lolists):
row.append(trees[i])
# Our list now has a new column containing the values from trees.
print(lolists)
# Legislators and birth_years have both been loaded in.
In [15]:
for i, e in enumerate(legislators):
e.append(birth_years[i])
In [16]:
# Define a list of lists
data = [["tiger", "lion"], ["duck", "goose"], ["cardinal", "bluebird"]]
# Extract the first column from the list
first_column = [row[0] for row in data]
apple_price = [100, 101, 102, 105]
In [17]:
apple_price_doubled = [2*p for p in apple_price]
apple_price_lowered = [p-100 for p in apple_price]
print(apple_price_doubled, apple_price_lowered)
In [20]:
for row in legislators:
try:
row[7] = int(row[7])
except ValueError as verr:
row[7] = None
# Hmm, but the above code fails.
# It fails because there is a value in the column that can't be converted to an int.
# Remember how some genders were missing? It also looks like some birthdays were missing, which is giving us invalid values in the birth years column.
In [22]:
# Cannot be parsed into an int with the int() function.
invalid_int = ""
# Can be parsed into an int.
valid_int = "10"
# Parse the valid int
try:
valid_int = int(valid_int)
except Exception:
# This code is never run, because there is no error parsing valid_int into an integer.
valid_int = 0
# Try to parse the invalid int
try:
invalid_int = int(invalid_int)
except Exception:
# The parsing fails, so we end up here.
# The code here will be run, and will assign 0 to invalid_int.
invalid_int = 0
print(valid_int)
print(invalid_int)
another_invalid_int = "Oregon"
another_valid_int = "1000"
try:
another_invalid_int = int(another_invalid_int)
except Exception as ex:
another_invalid_int = 0
try:
another_valid_int = int(another_valid_int)
except Exception as ex:
another_valid_int = 0
In [23]:
print(another_invalid_int, another_valid_int)
In [25]:
invalid_int = ""
try:
# This parsing will fail
invalid_int = int(invalid_int)
except Exception:
# Nothing will happen in the body of the except statement, because we are passing.
pass
# invalid_int still has the same value.
print(invalid_int)
# We can also use the pass statement with for loops.
# (although it's less useful in this example)
a = [1,4,5]
for i in a:
pass
# And if statements.
if 10 > 5:
pass
# We can use the pass keyword inside the body of any statement that ends with a colon.
valid_int = "10"
try:
valid_int = int(valid_int)
except:
pass
print(valid_int)
In [26]:
for row in legislators:
try:
row[7] = int(row[7])
except Exception as ex:
row[7] = 0
In [27]:
print(legislators)
In [28]:
data = [[1,1],[0,5],[10,7]]
last_value = 0
# There are some holes in this code -- it won't work properly if the first birth year is 0, for example, but its fine for now.
# It keeps track of the last value in the column in the last_value variable.
# If it finds an item that equals 0, it replaces the value with the last value.
for row in data:
# Check if the item is 0.
if row[0] == 0:
# If it is, replace it with the last value.
row[0] = last_value
# Set last value equal to the item -- we need to do this in order to keep track of what the previous value was, so we can use it for replacement.
last_value = row[0]
# The 0 value in the second row, first column has been replaced with a 1.
print(data)
In [29]:
last_birth_year = 0
for row in legislators:
if row[7] == 0:
row[7] = last_birth_year
last_birth_year = row[7]
Count up how many times each female name occurs in legislators. First name is the second column.
You'll need to make sure that gender (fourth column) equals "F", and that birth year (eighth column) is greater than 1940.
Store the first name key and the counts in the female_name_counts dictionary.
You'll need to use nested if statements to first check if gender and birth year are valid, and then to check if the first name is in female_name_counts.
In [30]:
names = ["Jim", "Bob", "Bob", "JimBob", "Joe", "Jim"]
name_counts = {}
for name in names:
if name in name_counts:
name_counts[name] = name_counts[name] + 1
else:
name_counts[name] = 1
female_name_counts = {}
In [31]:
female_name_counts = {}
for row in legislators:
if row[3] == 'F' and row[7] > 1940:
fname = row[1]
if fname in female_name_counts:
female_name_counts[fname] += 1
else:
female_name_counts[fname] = 1
In [34]:
# Set a variable equal to the None type
a = None
# A normal variable
b = 1
# This is True
print(a is None)
# And this is False
print(b is None)
# a is of the None type
print(type(a))
# Assigns whether a equals None to a_none
a_none = a is None
# Evaluates to True
print(a_none)
c = None
d = "Bamboo"
In [35]:
c_none, d_none = c is None, d is None
print(c_none, d_none)
In [36]:
max_val = None
data = [-10, -20, -50, -100]
for i in data:
# If max_val equals None, or i is greater than max_val, then set max_val equal to i.
# This ensures that no matter how small the values in data are, max_val will always get changed to a value in the list.
# If you are checking if a value equals None and you are using it with and or or, then the None check always needs to come first.
if max_val is None or i > max_val:
max_val = i
min_val = None
income = [100,700,100,50,100,40,56,31,765,1200,1400,32,6412,987]
In [37]:
min_val = None
for inc in income:
if min_val is None or inc < min_val:
min_val = inc
print(min_val)
Loop through the keys in female_name_counts, and get the value associated with the key.
Assign the value to max_value if it is larger, or if max_value is None.
At the end of the loop, max_value will be the largest value in the dictionary.
In [38]:
# female_name_counts has been loaded in.
max_value = None
In [39]:
for key in female_name_counts:
val = female_name_counts[key]
if max_value is None or val > max_value:
max_value = val
Loop through the keys in female_name_counts.
If any value equals 2, append the key to top_female_names.
At the end, top_female_names will be a list of the most occurring female congressperson names.
In [41]:
# female_name_counts has been loaded in.
top_female_names = []
In [42]:
for key in female_name_counts:
value = female_name_counts[key]
if value == 2:
top_female_names.append(key)
print(top_female_names)
In [43]:
animal_types = {"robin": "bird", "pug": "dog", "osprey": "bird"}
# The .items method lets us access a dictionary key and value in a loop.
for key,value in animal_types.items():
print(key)
print(value)
# This is equal to the value
print(animal_types[key])
plant_types = {"orchid": "flower", "cedar": "tree", "maple": "tree"}
In [44]:
for key, val in plant_types.items():
print(key)
print(val)
Loop through legislators, and count up how much each name where the gender column equals "M" and the birth year is after 1940 occurs. Store the results in a dictionary.
Then find the highest value in that dictionary.
Finally, loop through the dictionary and append any keys where the value equals the highest value to top_male_names.
In [45]:
# legislators has been loaded in.
top_male_names = []
In [49]:
male_name_counts = {}
print(len(legislators))
for row in legislators:
if row[3] == 'M' and row[7] > 1940:
fname = row[1]
if fname in male_name_counts:
male_name_counts[fname] += 1
else:
male_name_counts[fname] = 1
print(male_name_counts)
max_value = None
for key in male_name_counts:
val = male_name_counts[key]
if max_value is None or val > max_value:
max_value = val
for key, val in male_name_counts.items():
if val == max_value:
top_male_names.append(key)
print(top_male_names)
In [ ]: