gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:42:15 24 4
gpt4 key购买 nike

我有如下所示的 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

我想创建两个列表,

Category = [Category1, Category2, Category3]

Year = [1994, 1995, 1996, 1998]

我想省略列中的重复项。我正在阅读文件如下,

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']

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

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

关于python - 如何使用 CSV 文件的唯一值在 Python 中创建列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com