Python: Rename duplicates in list with progressive numbers without sorting list

前端 未结 7 2018
情书的邮戳
情书的邮戳 2020-12-13 18:41

Given a list like this:

mylist = [\"name\", \"state\", \"name\", \"city\", \"name\", \"zip\", \"zip\"]

I would like to rename the duplicate

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 19:28

    You can use hashtable to solve this problem. Define a dictionary d. key is the string and value is (first_time_index_in_the_list, times_of_appearance). Everytime when you see a word, just check the dictionary, and if the value is 2, use first_time_index_in_the_list to append '1' to the first element, and append times_of_appearance to current element. If greater than 2, just append times_of_appearance to current element.

提交回复
热议问题