Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #skrár: flights.txt -> lesa úr henni
- # inniheldur: nöfn lönd
- # notum split()
- # munum þurfa nota dict þar sem nöfnin eru key
- # 1. prenta út öll nöfn í stafrófsröð
- # 2. prenta út þau lönd sem þau hafa heimsótt
- # 3. prenta út nafn sá aðila sem hefur ferðast til flestra landa og hversu marga landa hann hvefur farið til
- #read_file -> skilar lista af listum
- #my_dict -> skilar dict nafn = key lönd = value
- #max_flyers
- """
- def check_file(filename):
- try:
- with open(filename, 'r') as f:
- return f
- except FileNotFoundError:
- return None
- """
- def read_file(filename):
- flyer_list = []
- with open(filename, 'r') as f:
- for line in f:
- flyer_list.append(line.split())
- return flyer_list
- def make_dict(flyer_list):
- flight_dict = {}
- # loop through the inner list of our f_list
- #check if names in innerlist are in dict, if so add country to the key
- for inner_list in flyer_list:
- name = inner_list[0]
- country = inner_list[1]
- if name in flight_dict:
- flight_dict[name].append(country)
- else:
- flight_dict[name] = [country]
- return flight_dict
- def clean_dict(flight_dict):
- #flight_dict = sorted(flight_dict)
- for flyer in flight_dict: #for flyer, country þá sækir bæði key og value
- flight_dict[flyer] = sorted(set(flight_dict[flyer]))
- return sorted(flight_dict.items())
- def print_flight(final_dict):
- for t in final_dict:
- print(t[0] + ':')
- country_list = t[1]
- for country in country_list:
- print('\t'+country)
- def frequent_flyer(flight_list):
- max_val = 0
- max_name = ''
- for t in flight_list:
- name = t[0]
- country_list = t[1]
- curr_val = len(country_list)
- if curr_val > max_val:
- max_val = curr_val
- max_name = name
- print('{} has been to {} countries'.format(max_name, max_val))
- def main():
- #read from file
- filename = ('/Users/kingamaris/Desktop/2019-T-111-PROG-master 3/projects/p9/flights.txt')
- if filename is None:
- print()
- else:
- flyer_list = read_file(filename)
- flight_dict = make_dict(flyer_list)
- flight_list = clean_dict(flight_dict)
- print_flight(flight_list)
- frequent_flyer(flight_list)
- #make dict from list of lists from file
- #print names
- #print names and countries
- #print frequent flyer
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement