|  | 
 
| 
>>> list =[123,['小甲鱼','牡丹'],456]
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  >>> 123 in list
 True
 >>> '小甲鱼' in list
 False
 >>> '小甲鱼' in list[1]
 True
 >>> list[1][1]
 '牡丹'
 
 这个是课程上老师写的完全可以执行,可为什么换成数字的列表就不行
 以下是自己写的:
 >>> test = [1,2,3,[4,5]]
 >>> test
 [1, 2, 3, [4, 5]]
 >>> 1 in test
 True
 >>> 4 in test
 False
 >>> 4 in test[1]
 Traceback (most recent call last):
 File "<pyshell#50>", line 1, in <module>
 4 in test[1]
 TypeError: argument of type 'int' is not iterable
 >>> test [1][1]
 Traceback (most recent call last):
 File "<pyshell#51>", line 1, in <module>
 test [1][1]
 TypeError: 'int' object is not subscriptable
 
 本帖最后由 jackz007 于 2019-6-20 14:09 编辑 
 列表中的元素是有顺序的,就是说,元素与索引之间是一一对应的关系,在楼主的例子中,只有 test[3] 才是列表([4 , 5])。
 4 in test[3] 结果是 True
 test[3][1] == 5 结果也会是 True
 test[1] 不是列表,所以,不可以被当成列表来访问,前面已经说过,只有 test[3] 才可以。
 
 | 
 |