Delete item in a list using a for-loop

前端 未结 5 1201
感情败类
感情败类 2021-01-13 17:35

I have an array with subjects and every subject has connected time. I want to compare every subjects in the list. If there are two of the same subjects, I want to add the ti

5条回答
  •  长情又很酷
    2021-01-13 18:08

    The best practice is to make a new list of the entries to delete, and to delete them after walking the list:

    to_del = []
    subjectlength = 8
    for x in range(subjectlength):
        for y in range(x):
            if subject[x] == subject[y]:
                #add
                time[x] = time[x] + time[y]
                to_del.append(y)
    
    to_del.reverse()
    for d in to_del:
        del subject[d]
        del time[d]
    

提交回复
热议问题