使对象可迭代? [英] Making objects iterable?
问题描述
我试图遍历列表列表中的每一行,将每行的元素追加到新列表中,然后在新列表中找到唯一的元素。
I'm trying to iterate over each row in a list of lists, append an element from each row to a new list, then find the unique elements in the new list.
我知道我可以使用for循环轻松地做到这一点。我正在尝试另一种方法,因为我想了解有关类和函数的更多信息。
I understand that I can do this easily with a for loop. I'm trying a different route because I want to learn more about classes and functions.
以下是列表列表的示例。第一行是标题:
Here's an example of the list of lists. The first row is the header:
legislators = [
['last_name', 'first_name', 'birthday', 'gender', 'type', 'state', 'party'],
['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration'],
['Bland', 'Theodorick', '1742-03-21', '', 'rep', 'VA', ''],
['Burke', 'Aedanus', '1743-06-16', '', 'rep', 'SC', ''],
['Carroll', 'Daniel', '1730-07-22', 'M', 'rep', 'MD', ''],
['Clymer', 'George', '1739-03-16', 'M', 'rep', 'PA', ''],
['Contee', 'Benjamin', '', 'M', 'rep', 'MD', ''],...]
这是我的代码:
import csv
f = open("legislators.csv")
csvreader = csv.reader(f)
legislators = list(csvreader)
class Dataset:
def __init__(self, data):
self.header = data[0] #Isolate header from CSV file
self.data = data[1:] #Subset CSV data to remove header
legislators_dataset = Dataset(legislators)
def the_set_maker(dataset):
gender = []
for each in dataset:
gender.append(each[3])
return set(gender)
t=the_set_maker(legislators_dataset)
print(t)
我明白了以下错误:
TypeErrorTraceback (most recent call last)
<ipython-input-1-d65cb459931b> in <module>()
20 return set(gender)
21
---> 22 t=the_set_maker(legislators_dataset)
23 print(t)
<ipython-input-1-d65cb459931b> in the_set_maker(dataset)
16 def the_set_maker(dataset):
17 gender = []
---> 18 for each in dataset:
19 gender.append(each[3])
20 return set(gender)
TypeError: 'Dataset' object is not iterable
我认为答案是尝试使用 def __iter __(self)<创建方法/ code>在我的
Dataset
类中,但是我无法使其正常工作。这是正确的轨道吗?如果不是,哪个更好?
I think the answer is to try to create a method using def __iter__(self)
in my Dataset
class, but I haven't been able to get this to work. Is this the right track? If not, what's a better one?
推荐答案
根据 __ iter __
的文档:
According to the documentation for __iter__
:
此方法应返回一个新的迭代器对象,该对象可以遍历容器中的所有对象。
This method should return a new iterator object that can iterate over all the objects in the container.
您可以尝试以下类定义:
You might try the following class definition:
class Dataset:
def __init__(self, data):
self.header = data[0] #Isolate header from CSV file
self.data = data[1:] #Subset CSV data to remove header
def __iter__(self):
return iter(self.data)
如果您愿意尝试新选项时,请考虑使用熊猫:
If you're open to trying new options, consider using Pandas:
import pandas as pd
df = pd.read_csv('legislators.csv')
t=df['gender']
或者,如果您确实有ant自己读取CSV文件,
Or, if you really want to read in the CSV yourself,
df = pd.DataFrame(legislators[1:], columns=legislators[0])
这篇关于使对象可迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!