如何在Python中使用CSV文件的唯一值创建列表? [英] How to create a list in Python with the unique values of a CSV file?

查看:78
本文介绍了如何在Python中使用CSV文件的唯一值创建列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的CSV文件,

I have CSV file that looks like the following,

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

我要创建两个列表

Category = [Category1, Category2, Category3]

Year = [1994, 1995, 1996, 1998]

我想省略该列中的重复项.我正在读取文件,如下所示

I want to omit the duplicates in the column. I am reading the file as following,

DataCaptured = csv.reader(DataFile, delimiter=',')  
DataCaptured.next()

然后遍历

   for Column in DataCaptured:

推荐答案

您可以这样做:

DataCaptured = csv.reader(DataFile, delimiter=',', skipinitialspace=True) 

Category, Year = [], []
for row in DataCaptured:
    if row[0] not in Year:
        Year.append(row[0])
    if row[1] not in Category:
        Category.append(row[1])    

print Category, Year        
# ['Category1', 'Category2', 'Category3'] ['1994', '1995', '1996', '1998']


如评论中所述,如果顺序无关紧要,则使用集合会更容易,更快捷:


As stated in the comments, if order does not matter, using a set would be easier and faster:

Category, Year = set(), set()
for row in DataCaptured:
    Year.add(row[0])
    Category.add(row[1])

这篇关于如何在Python中使用CSV文件的唯一值创建列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆