0

我正在尝试创建一个自定义迭代器类来运行用户数据的 JSON 文件。

class trainUsers():
    def __getitem__(self, num):
        with open(file.json') as f:
            #if num > 43873:
             #   raise IndexError("end of file")
            train_user = [json.loads(line) for line in f]
        return train_user[num]
    def rating(self,num):
        return self[num]['rating']
    def id(self,num):
        return self[num]['user_id']

然后我可以通过

trainuser = data.trainUsers()

trainuser[num] #returns a dict
trainuser.rating(num)

但是,我遇到了迭代问题。

count = 0
rating = 0
for i in trainuser:
    rating += trainuser.rating(i)
    count += 1
print stars/ count

IndexError 被注释掉,因为它总是被调用,我认为文档说 for 循环需要 IndexError 才能运行。上面给出了getitem函数的以下错误。

@return train_user[num]
TypeError: list indices must be integers, not dict

我知道 trainuser 正在返回字典,但我无法概念化为什么 for 循环使用字典进行迭代。

如何使此类既可以迭代又可以单独调用每个字典?

4

1 回答 1

0

This is because your 'for' loop is returning each item from the json file as 'i' instead of the for loops index.

An example of a 'for' loop in python

>>> arr = ['hip','hello','hey']
>>> for i in arr:
>>>    print i
hip
hello
hey

In your case this should work:

>>> rating = 0
>>> for i in trainuser:
>>>     rating += trainuser['rating']
>>>
>>> print stars/ len(trainuser)

Or if you'd rather keep your method of finding the row in the json file by the index you can use enumerate:

>>> rating = 0
>>> for index, i in enumerate(trainuser):
>>>     rating += trainuser.rating(index)
>>>
>>> print stars/ len(trainuser)
于 2013-07-05T22:39:25.513 回答