How to create a list in Python with the unique values of a CSV file?

前端 未结 3 757
长发绾君心
长发绾君心 2020-12-06 17:38

I have CSV file that looks like the following,

1994, Category1, Something Happened 1
1994, Category2, Something Happened 2
1995, Category1, Something Happen         


        
3条回答
  •  一个人的身影
    2020-12-06 17:46

    dawg pointed out one of the greatest tricks in Python: using set() to remove duplicates from a list. dawg shows how to build the unique list from scratch by adding each item to a set, which is perfect. But here's another equivalent way to do it, generating a list with duplicates and a list without duplicates using a list(set()) approach:

    import csv
    
    in_str = [
        'year, category, event',
        '1994, Category1, Something Happened 1',
        '1994, Category2, Something Happened 2',
        '1995, Category1, Something Happened 3',
        '1996, Category3, Something Happened 4',
        '1998, Category2, Something Happened 5'
        ]
    
    cdr = csv.DictReader(in_str, skipinitialspace=True)
    
    col = []
    for i in cdr:
        col.append(i['category'])
    
    # all items in the column...
    print(col)
    # only unique items in the column...
    print(list(set(col)))
    

提交回复
热议问题