![](/img/trans.png)
[英]AttributeError: '***' object has no attribute '***_set'
[英]AttributeError: 'set' object has no attribute 'b'
我有以下代碼:
N1 = int(input())
a = set(list(map(int, input().split())))
N2 = int(input())
for i in range(N2):
b = input().split()
c = set(list(map(int, input().split())))
a.b[0](c)
print(sum(a))
使用典型的輸入,列表b
如下所示:
b = ['intersection_update', '10']
ab[0](c)
什么問題? 顯然我沒有正確評估它。
這個概念似乎很好,但似乎set a
無法采用實際上是列表元素的屬性。
我要評估的是:
a.intersection_update(c)
這是我得到的錯誤:
Traceback (most recent call last):
File "solution.py", line 7, in
a.b[0](c)
AttributeError: 'set' object has no attribute 'b'
我認為您想使用getattr
來獲取一個屬性,該屬性的名稱作為字符串存儲在另一個變量中:
getattr(a, b[0])(c)
您當前的代碼是尋找一個名為屬性b
的a
集。
您無法在Python中使用點運算符進行這種間接屬性訪問。 使用getattr()
代替:
>>> a = {1, 2, 3, 4, 5}
>>> c = {3, 4, 5, 6, 7}
>>> b = ['intersection_update', '10']
>>> getattr(a, b[0])(c)
>>> a
{3, 4, 5}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.