I'm trying to iterate a list that contains some duplicate elements. I'm using the amount of duplicates so I don't want to put the list in a set before I iterate over the list.
I'm trying to count how many times the element appears and then write the element (the name) and the count of how many times it appears.
The issue I am running into is that in my output CSV file, there are as many rows as there are times the element appears. I am writing the CSV to an HTML table after its completed so I want it to be deduplicated.
My end goal is to have it count how many times the name appears, then write a row to the CSV file that contains the name and the count, then move to the next name in the list.
I tried searching and came across itertools.groupby
but I'm not sure if that's going to be useful in this instance and if it is, how to use it correctly.
Thanks for the help.
EDIT: I forgot to mention - Python 2.6
with open(sys.argv[1]) as infile:
rdr = csv.DictReader(infile, dialect='excel')
qualsin = []
headers = ['Qualifier Name','Appointments']
for row in rdr:
row['Qualifier Name'] = row['Qualifier Name'].upper()
qualsin.append(row['Qualifier Name'])
qualsin.sort()
#total = 0
with open('tempwork.csv', 'w') as tempwork:
wrtr = csv.writer(tempwork, dialect='excel')
wrtr.writerow(headers)
for quals in qualsin:
d = [quals, qualsin.count(quals)]
#a = dict((key, value) for (key, value) in d)
#total += qualsin.count(quals)
wrtr.writerow(d)