collecting all the indices of unique elements in a csv file and populating them in a row

This would be fairly straight forward with some functions from the standard library: collections.defaultdict. csv.reader, and itertools.count. Something like:

import csv
import collections 
import itertools

data = collections.defaultdict(list)                                                                        

index = itertools.count(1)
with open('D:/TABLE/unique_values.csv') as f1:
    reader = csv.reader(f1)

    for row in reader:
        for value in row:
            data[value].append(next(index))    
            
for unique_value, indices in data.items():
    print(f"{unique_value}:", *indices) 

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top