如何使用 CSV 文件的唯一值在 Python 中创建列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24441606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:37:13  来源:igfitidea点击:

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

pythonlistcsvunique

提问by Gravity M

I have CSV file that looks like the following,

我有如下所示的 CSV 文件,

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

I want to create two lists,

我想创建两个列表,

Category = [Category1, Category2, Category3]

and

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()

and Looping through,

并循环通过,

   for Column in DataCaptured:

采纳答案by dawg

You can do:

你可以做:

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:

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

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

回答by CT Zhu

A very concise way to do this is to use pandas, the benefits are: it has a faster CSV pharser; and it works in columns (so it only requires one df.apply(set)to get you there) :

一个非常简洁的方法是使用pandas,好处是:它有一个更快的 CSV 分析器;并且它在列中工作(所以它只需要一个df.apply(set)就能让你到达那里):

In [244]:
#Suppose the CSV is named temp.csv
df=pd.read_csv('temp.csv',header=None)
df.apply(set)
Out[244]:
0                        set([1994, 1995, 1996, 1998])
1            set([ Category2,  Category3,  Category1])
2    set([ Something Happened 4,  Something Happene...
dtype: object

The downside is that it returns a pandas.Series, and to get access each list, you need to do something like list(df.apply(set)[0]).

缺点是它返回一个pandas.Series,并且要访问每个列表,您需要执行类似list(df.apply(set)[0]).

Edit

编辑

If the order has to be preserved, it can be also done very easily, for example:

如果必须保留顺序,也可以很容易地完成,例如:

for i, item in df.iteritems():
    print item.unique()

item.unique()will return numpy.arrays, instead of lists.

item.unique()将返回numpy.arrays,而不是lists。

回答by rob_7cc

dawgpointed out one of the greatest tricks in Python: using set()to remove duplicates from a list. dawgshows 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:

dawg指出了 Python 中最伟大的技巧之一:使用set()从列表中删除重复项。 dawg展示了如何通过将每个项目添加到 a 来从头开始构建唯一列表set,这是完美的。但这是另一种等效的方法,使用一种list(set())方法生成一个有重复项的列表和一个没有重复项的列表:

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)))