Tasks week 39 Flashcards
(9 cards)
Write a command that prints the list of all
persons who were born in “Copenhagen”
AND followed the occupation as “judge”!
def cities(persons, city): worksome = [] for item in persons: if item["p"] == city: worksome.append(item) return worksome
def workingin(persons,occupations): worksome = [] for p in persons: if occupations in p['o']: worksome.append(p) return worksome
workvariable = cities(dbl,”Copenhagen”)
print(workingin(workvariable,”judge”))
Write a function occupied_as that takes
a list of persons and an occupation name,
and returns a list of all persons who had
that occupation!
def occupied_as(persons, occupation):
name_occu = []
for items in persons:
if occupation in items[“o”]:
name_occu.append(items) return name_occu print (occupied_as(dbl,"writer"))
Write a function born_in that takes a list
of persons and a city name, and returns a
list of all persons born in that city
def born_in(persons, city): persons2 = [] for items in persons: if items["p"] == city: persons2.append(items) return persons2 print (born_in(dbl,"Odense"))
Write a function ratio that takes a list of
persons, and determines and prints their
gender ratio!
def ratio(): male = 0 female = 0 for item in dbl: if item["g"] == "m": male += 1 elif item["g"] == "f": female += 1
print (male/(male + female)* 100, “% men”)
print (female/(male + female)* 100, “% women”)
ratio()
Write a function aristocrats(persons) that returns a list of persons who
have an “ af “, “ von “, “ van “ or “ de “ in their name!
def aristocrats(persons): aris = [] for person in persons: if " af " in person["name"]: aris.append(person) elif " von " in person["name"]: aris.append(person) elif " van " in person["name"]: aris.append(person) elif " de " in person["name"]: aris.append(person) return aris print (aristocrats(dbl))
Write a function born_rural(persons) that returns a list of persons who have
not been born in either Copenhagen, Aarhus or Odense!
def born_rural(persons): rural = [] citylist = ["Copenhagen", "Odense", "Aarhus", ""] for r in persons: if r["p"] not in citylist: rural.append(r) return rural print (born_rural(dbl))
Write a function older_than(persons,age) that returns a list of persons who
reached an age older than age. Test your function by printing all persons who
reached an age of at least 90 years!
def older_than(persons,age): bornanddeath = [] for items in persons: if len(items["l"]) == 9 and int(items["l"][5:9]) - int(items["l"][0:4]) >= age: bornanddeath.append(items) return bornanddeath print (older_than(dbl,89))
Write a function painters(persons) that returns a list of all painters who
had their mean year of activity in the Danish golden age between 1800 and
1850!
def painters(persons): job = [] goldenage = [] for i in range(1800, 1850+1): goldenage.append(i) for item in persons: if "painter" in item["o"] and item["y"] in goldenage: job.append(item) return job
def print_people(person): for person in persons: print (painters(dbl))
Write a function incomplete(persons) that returns a list containing three
lists: the rst one contains all persons without years of birth and death, the
second contains all persons with only year of birth, and the third contains all
persons with only year of death!
def incomplete(persons): no_years = [] no_yearscount = 0 only_birth = [] only_birthcount = 0 only_death = [] only_deathcount = 0 for person in persons: if person["l"] == "-": no_years.append(person) elif person["l"][0] == "-": only_death.append(person) elif person["l"][-1] == "-": only_birth.append(person) return no_years, only_birth, only_death print (incomplete(dbl))