Delete item in a list using a for-loop

前端 未结 5 1028
感情败类
感情败类 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:14

    If the elements of subject are hashable:

    finalinfo = {}
    
    for s, t in zip(subject, time):
      finalinfo[s] = finalinfo.get(s, 0) + t
    

    This will result in a dict with subject: time key-value pairs.

提交回复
热议问题