Generation

generate functionMon, 26 Dec 2022

There are two lists. Write a function that looks for matching values of these two lists. If a match is found, replace with replacing_num (but only the 2nd value) stnums = ['4004'] students = [ ['0001', 'Antonov', 'Anton', 'Igorevich', '20.08.2009', 'BST161'], ["1102", "Bogov", "Artem", "Igorevich", "25.01.2010", "BST162"] ["0333", "Glagoleva", "Anastasiya", "Nikolaevna", "11.07.2009", "BST163"] ["4004", "Stepanova", "Natalia", "Aleksandrovna", "13.02.2008", "BST161"] ["0607", "Siropova", "Violetta", "Eduardovna", "28.05.2010", "BST162"], ["4004", "Potapov", "Dmitry", "Stanislavovich", "14.02.2012", "BST161"] ] replacing_num = '9090' Example: In the list of stnums we see the value '4004'. It occurs twice in the list of students. We need to change the second one to replacing_num. The result should be this: [ ... ["4004", "Stepanova", "Natalia", "Aleksandrovna", "13.02.2008", "BST161"] ... ["9090", "Potapov", "Dmitry", "Stanislavovich", "14.02.2012", "BST161"] ]

def replace_sec(stnums, students, replacing_num): for stnum in stnums: num = 0 for student in students: if stnum == student[0]: num += 1 if num > 1: student[0] = replacing_num return students replace_sec(stnums, students, replacing_num)

Questions about programming?Chat with your personal AI assistant