In this assignment, you are asked to demonstrate a program which uses the container class in Python, i.e. list and dict, to automatically find all the duplicated book items in the UTAR library collection. You can only use the Python built-in functions to perform the task. Use of any advanced modules other than csv, such as pandas and numpy, will immediately lead to a zero mark.
Your task in this assignment is to find out the total number of duplicated titles in the input file called library-titles.csv.
import csv
path_to_csv = r"\library-titles.csv"
def get_count_titles(titles):
result_dict = dict.fromkeys(set(titles))
for key in result_dict:
result_dict[key] = titles.count(key)
return result_dict
def get_all_titles(path_to_csv):
with open(path_to_csv) as csv_file:
reader = csv.reader(csv_file)
all_titles = []
for row in reader:
all_titles.extend(row)
return all_titles
def main():
all_titles = get_all_titles(path_to_csv)
dict_count_title = get_count_titles(all_titles)
total_number_duplicated_titles = 0
for title, count in dict_count_title.items():
if count > 1:
total_number_duplicated_titles += count
print(f"COUNT | {count} \t DUPLICATE TITLE - '{title}'")
print("-" * 50)
print(f"Total number of duplicate titles - {total_number_duplicated_titles}")
if __name__ == "__main__":
main()
Comments
Leave a comment