Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下代码
d1={'key1':[1,2,3],'key2':[4,5,6]} d1['key2'][0][2]
产生此错误:
TypeError:“int”对象不可下标
我希望输出是4,6
4,6
您正在对一个整数(在本例中为 4)的列表项使用订阅操作,因此出现错误。
您可以使用列表切片获得所需的输出:
In [193]: d1 = {'key1': [1,2,3], 'key2': [4,5,6]} In [194]: d1['key2'] Out[194]: [4, 5, 6] In [195]: d1['key2'][0::2] # [start:stop:step] Out[195]: [4, 6]