Python 类型错误:“int”对象没有属性“__getitem__”错误,因为书中可能存在错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30522420/
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 08:35:20  来源:igfitidea点击:

TypeError: 'int' object has no attribute '__getitem__' error because of possible erratum in book

python

提问by megashigger

I'm going through the new book "Data Science from Scratch: First Principles with Python" and I think I've found an errata.

我正在阅读新书“从头开始的数据科学:Python 的首要原则”,我想我找到了一个勘误表。

When I run the code I get "TypeError: 'int' object has no attribute '__getitem__'".I think this is because when I try to select friend["friends"], friendis an integer that I can't subset. Is that correct? How can I continue the exercises so that I get the desired output? It should be a list of friend of friends (foaf). I know there's repetition problems but those are fixed later...

当我运行代码时,我"TypeError: 'int' object has no attribute '__getitem__'".认为这是因为当我尝试选择时friend["friends"], ,friend是一个我不能子集的整数。那是对的吗?我怎样才能继续练习以获得所需的输出?它应该是朋友的朋友列表(foaf)。我知道存在重复问题,但这些问题稍后会修复...

users = [
    {"id": 0, "name": "Ashley"},
    {"id": 1, "name": "Ben"},
    {"id": 2, "name": "Conrad"},
    {"id": 3, "name": "Doug"},
    {"id": 4, "name": "Evin"},
    {"id": 5, "name": "Florian"},
    {"id": 6, "name": "Gerald"}
]

#create list of tuples where each tuple represents a friendships between ids
friendships = [(0,1), (0,2), (0,5), (1,2), (1,5), (2,3), (2,5), (3,4), (4,5), (4,6)]

#add friends key to each user 
for user in users:
    user["friends"] = []

#go through friendships and add each one to the friends key in users
for i, j in friendships:
    users[i]["friends"].append(j)
    users[j]["friends"].append(i)

def friends_of_friend_ids_bad(user): 
    #foaf is friend of friend
    return [foaf["id"]
        for friend in user["friends"]
        for foaf in friend["friends"]]

print friends_of_friend_ids_bad(users[0])

Full traceback:

完整追溯:

Traceback (most recent call last):
  File "/Users/marlon/Desktop/test.py", line 57, in <module>
    print friends_of_friend_ids_bad(users[0])
  File "/Users/marlon/Desktop/test.py", line 55, in friends_of_friend_ids_bad
    for foaf in friend["friends"]]
TypeError: 'int' object has no attribute '__getitem__'
[Finished in 0.6s with exit code 1]
[shell_cmd: python -u "/Users/marlon/Desktop/test.py"]
[dir: /Users/marlon/Desktop]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

How I think it can be fixed: I think you need users as a second argument and then do "for foaf in users[friend]["friends"]" instead of "for foaf in friend["friends"]

我认为如何解决:我认为您需要用户作为第二个参数,然后执行“for foaf in users[friend]["friends"]”而不是“for foaf infriend["friends"]

采纳答案by Alik

Yes, you've found an incorrect piece of code in the book.

是的,您在书中发现了一段不正确的代码。

Implementation for friends_of_friend_ids_badfunction should be like this:

friends_of_friend_ids_bad函数的实现应该是这样的:

def friends_of_friend_ids_bad(user): 
    #foaf is friend of friend
    return [users[foaf]["id"]
        for friend in user["friends"]
        for foaf in users[friend]["friends"]]

user["friends"]is a list of integers, thus friendis an integer and friend["friends"]will raise TypeErrorexception

user["friends"]是一个整数列表,因此friend是一个整数并且friend["friends"]会引发TypeError异常



UPD

UPD

It seems, that the problem in the book was not about friends_of_friend_ids_badfunction but about populating friendslists.

看来,书中的问题不在于friends_of_friend_ids_bad函数,而在于填充friends列表。

Replace

代替

for i, j in friendships:
    users[i]["friends"].append(j)
    users[j]["friends"].append(i)

with

for i, j in friendships:
    users[i]["friends"].append(users[j])
    users[j]["friends"].append(users[i])

Then friends_of_friend_ids_badand friends_of_friend_idswill work as intended.

然后friends_of_friend_ids_badfriends_of_friend_ids将按预期工作。

回答by Ericson Willians

The error is on:

错误是:

return [foaf["id"] for friend in user["friends"] for foaf in friend["friends"]]

In the second for loop, you're trying to access __getitem__of users[0]["friends"], which is exactly 5 (ints don't have __getitem__).

在第二个 for 循环中,您尝试访问__getitem__of users[0]["friends"],正好是 5(整数没有__getitem__)。

You're trying to store on the list foaf["id"]for each friend in user["friends"]and for each foaf friend["friends"]. The problem is that foaf gets from friend["friends"]the number from a tuple inside friendships that was stored on users, and then you try to access ["id"]from it, trying to call __getitem__from an integer value.

您正在尝试foaf["id"]为每个朋友user["friends"]和每个 foaf存储在列表中friend["friends"]。问题是 foaf 从friend["friends"]存储在用户中的友谊内部的元组中获取数字,然后您尝试从中访问["id"],尝试__getitem__从整数值调用。

That's the exact cause of your problem.

这就是您问题的确切原因。