So i have a list with 1000 sub-list where the sub-lists have a date in this format "2022-01-01" and an index to another list. So the list loks like this [["2022-01-01", 3], ["2010-01-01", 1], ["2022-01-01", 12]] with 1000 elements.
What i would like to get is a new list with sub-list that have the index of the dates that are equal.
So the output should look like this [[3, 12,]].
I have try'd
count = 0
for i in range(len(dateList)):
if i != x:
if dateList[i][0] == dateList[x][0]:
print(dateList[i][0], dateList[x][0])
count = 1
or
for i in range(len(dateList)):
for x in range(len(dateList)):
if 1!= x:
if dateList[i][0] == dateList[x][0]:
print(dateList[i][0], dateList[x][0])
I understand why both are wrong, I used them to try to get my thoughts to the rigth answer. However I cant seem to find any solution.
CodePudding user response:
You can use a defaultdict to create a data structure keyed by the dates and whose values are the indices where those dates occur. Once you create such a thing, you can iterate over its values, selecting the ones which have more than one entry:
from collections import defaultdict
date_list = [["2022-01-01", 3], ["2010-01-01", 1], ["2022-01-01", 12]]
d = defaultdict(list)
for i,date_pair in enumerate(date_list):
date = date_pair[0]
d[date].append(i)
duplicates = [v for v in d.values() if len(v) > 1]
print(duplicates) #[[0,2]]