Finding repeats in multiple lists read from CSV File (Python)
Question: Title seems confusing, but let's say I'm working with the following CSV file
('names.csv').

    
    
        name1,name2,name3
        Bob,Jane,Joe
        Megan,Tom,Jane
        Jane,Joe,Rob
    

My question is, how would I go about making code that returns the string that
occurs at least 3 times. So the output should be 'Jane', because that occurs
at least 3 times. Really confused here.. perhaps some sample code would help
me better understand?

So far I have:

    
    
        import csv
        reader = csv.DictReader(open("names.csv"))
    
        for row in reader:
            names = [row['name1'], row['name2'], row['name3']]
            print names
    

This returns:

    
    
        ['Bob', 'Jane', 'Joe']
        ['Megan', 'Tom', 'Jane']
        ['Jane', 'Joe', 'Rob']
    

Where do I go from here? Or am I going about this wrong? I'm really new to
Python (well, programming altogether), so I have close to no clue what I'm
doing..

Cheers


Answer: I'd do it like this:

    
    
    >>> from collections import defaultdict
    >>> d = defaultdict(int)
    >>> rows = [['Bob', 'Jane', 'Joe'],
    ... ['Megan', 'Tom', 'Jane'],
    ... ['Jane', 'Joe', 'Rob']]
    ...
    >>> for row in rows:
    ...     for name in row:
    ...         d[name] += 1
    ... 
    >>> filter(lambda x: x[1] >= 3, d.iteritems())
    [('Jane', 3)]
    

It uses dict with default value of 0 to count how many times each name happens
in the file, and then it filters the dict with according condition (count >=
3).

