Generation

generate functionMon, 26 Dec 2022

There are 2 functions. One looks for duplicate values and makes a dictionary. The second changes the duplicates to the desired value. The first function (find_duplicates) works correctly. But the second function (change_duplicates) is incomplete. Finish the code so that the duplicate student number changes to the one in brackets in the change_duplicates function. def find_duplicates(lst): stnums = [] cnt = {} for el in lst: stnums.append(el[0]) for i in stnums: if i in cnt: cnt[i] += 1 else: cnt[i] = 1 return cnt def change_duplicates(lst, replacing_num): stnums = [] duplicates = find_duplicates(lst) for k, v in duplicates.items(): if v > 1: stnums = [k] for student in lst: if stnums in lst: #need right code#

#Python 3.7.3 #https://stackoverflow.com/questions/57774616/find-duplicates-in-list-of-tuples-in-python def find_duplicates(lst): stnums = [] cnt = {} for el in lst: stnums.append(el[0]) for i in stnums: if i in cnt: cnt[i] += 1 else: cnt[i] = 1 return cnt def change_duplicates(lst, replacing_num): for stnums in lst: if stnums[0] == replacing_num: stnums[0] = replacing_num students = [["1", "John", "Biology", "A"], ["2", "Mary", "Math", "C"], ["3", "Alex", "Computer Science", "B"], ["3", "Alex", "Computer Science", "B"]] print(students)

Questions about programming?Chat with your personal AI assistant