嘗試在列表中找到重複的字符串〜100,000,並計算它們各自的位置以及它們所在的索引並打印它們。到目前爲止,我想出了這一點:從大列表中打印每組重複字符串及其索引的編號
list_b = ['04/Sep/2016:00:00:03 -0400', '04/Sep/2016:00:00:04 -0400', '04/Sep/2016:00:00:05 -0400', '04/Sep/2016:00:00:06 -0400', '04/Sep/2016:00:00:06 -0400', '04/Sep/2016:00:00:08 -0400', '04/Sep/2016:00:00:08 -0400', '04/Sep/2016:00:00:08 -0400', '04/Sep/2016:00:00:11 -0400', '04/Sep/2016:00:00:15 -0400', '04/Sep/2016:00:00:19 -0400', '04/Sep/2016:00:00:20 -0400', '04/Sep/2016:00:00:23 -0400', '04/Sep/2016:00:00:25 -0400', '04/Sep/2016:00:00:26 -0400']
for i in list_b:
if(i in list_b):
print(i + " Amount of duplicates: " + amount of duplicates + " Index of duplicates: " + index of duplicate)
輸出應該是這樣的:
"04/Sep/2016:00:00:06 -0400 Amount of duplicates: 2 Index of duplicates: 3,4"
"04/Sep/2016:00:00:08 -0400 Amount of duplicates: 3 Index of duplicates: 5,6,7"
如果你創建一個字典,鍵爲'timestamp'和值作爲你遇到過多少次了'count'?但是,然後創建字典將是一個「O(n)」操作,並且打印也將是一個「O(n)」操作,反正它大約是'O(n)'。如果那是你的事情,我會寫一些代碼。 –
你確定使用第三方庫嗎? – trendsetter37