AttributeError: ‘list’ 对象没有属性 ‘get’

AttributeError: ‘list’ 对象没有属性 ‘get’

AttributeError: ‘list’ object has no attribute ‘get’

Python “AttributeError: ‘list’ object has no attribute ‘get’” 发生在我们get()在列表而不是字典上调用方法时。要解决该错误,请调用get()字典,例如通过访问特定索引处的列表或遍历列表。

attributeerror-list-object-has-no-attribute-get

下面是错误如何发生的示例。

主程序
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] # ⛔️ AttributeError: 'list' object has no attribute 'get' my_list.get('name')

我们创建了一个包含 3 个字典的列表,并尝试调用get()列表中导致错误的方法,因为这get()是一个字典方法。

解决该错误的一种方法是访问特定索引处的列表元素。

主程序
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] result = my_list[0].get('name') print(result) # 👉️ "Alice"

我们访问了索引处的列表元素,0并使用该get()方法获取name键的值。

如果您需要在列表中查找字典,请使用生成器表达式。

主程序
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] result = next( (item for item in my_list if item['name'] == 'Bob'), {} ) print(result) # 👉️ {'id': 2, 'name': 'Bob'} print(result.get('name')) # 👉️ "Bob" print(result.get('id')) # 👉️ 2

示例中的生成器表达式查找name具有值为 的键Bob的字典并返回该字典。

我们使用一个空字典作为回退,所以如果列表中的字典有一个值为 的键,生成器表达式将返回一个空字典。 nameBob

如果您需要在列表中查找所有符合条件的词典,请使用该
filter()函数。

主程序
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Alice'}, ] new_list = list( filter(lambda person: person.get('name') == 'Alice', my_list) ) # 👇️ [{'id': 1, 'name': 'Alice'}, {'id': 3, 'name': 'Alice'}] print(new_list)

If you need to use the get() method on each dictionary in a list, use a for
loop to iterate over the list.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] new_list = [] for person in my_list: new_list.append(person.get('name')) print(person.get('name')) print(new_list) # 👉️ ['Alice', 'Bob', 'Carl']

We used a for loop to iterate over the list and on each iteration used the
get() method to get the value of the name property of the dictionary.

The example can be rewritten using a list comprehension.

main.py
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] new_list = [person.get('name') for person in my_list] print(new_list) # 👉️ ['Alice', 'Bob', 'Carl']

The dict.get method
returns the value for the given key if the key is in the dictionary, otherwise a
default value is returned.

The method takes the following 2 parameters:

Name Description
key The key for which to return the value
default The default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None,
so the get() method never raises a KeyError.

The “AttributeError: ‘list’ object has no attribute ‘get'” occurs when we try to call the get() method on a list instead of a dictionary.

You can view all the attributes an object has by using the dir() function.

main.py
my_list = ['a', 'b', 'c'] # 👉️ [... 'append', 'clear', 'copy', 'count', 'extend', 'index', # 'insert', 'pop', 'remove', 'reverse', 'sort' ...] print(dir(my_list))

If you pass a class to the
dir() function, it
returns a list of names of the class’s attributes, and recursively of the
attributes of its bases.

If you try to access any attribute that is not in this list, you would get the
“AttributeError: list object has no attribute” error.

Since get() is not a method implemented by lists, the error is caused.

结论

Python “AttributeError: ‘list’ object has no attribute ‘get’” 发生在我们get()在列表而不是字典上调用方法时。要解决该错误,请调用get()字典,例如通过访问特定索引处的列表或遍历列表。