鱼C论坛

 找回密码
 立即注册
查看: 1945|回复: 6

[已解决]元组的访问疑问

[复制链接]
发表于 2021-9-18 09:25:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. >>> c=os.walk('C:\\Users\\lyl\\AppData\\Local\\Programs\\Python\\Python39\\ccc')
  2. >>> c[:]
  3. Traceback (most recent call last):
  4.   File "<pyshell#13>", line 1, in <module>
  5.     c[:]
  6. TypeError: 'generator' object is not subscriptable
  7. >>> c[1]
  8. Traceback (most recent call last):
  9.   File "<pyshell#14>", line 1, in <module>
  10.     c[1]
  11. TypeError: 'generator' object is not subscriptable
  12. >>> list(c)
  13. [('C:\\Users\\lyl\\AppData\\Local\\Programs\\Python\\Python39\\ccc', [], ['123.txt', '1234.txt', '234.txt', '39.py', '40.py', '41.py', '42.py', '890.txt', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python39.dll', 'pythonw.exe'])]
  14. >>> c(1)
  15. Traceback (most recent call last):
  16.   File "<pyshell#16>", line 1, in <module>
  17.     c(1)
  18. TypeError: 'generator' object is not callable
  19. >>>
复制代码


为什么无法访问元组里面的元素呢,如果要访问应该如何操作?
最佳答案
2021-9-18 10:22:23
全桥整流 发表于 2021-9-18 09:52
>>> c=os.walk('C:\%users\\lyl\\AppData\\Local\\Programs\\Python\\Python39\\ccc')
>>> list(c)[0][1 ...

先转列表,增设一个变量xx=list(c)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-18 09:30:33 From FishC Mobile | 显示全部楼层
本帖最后由 wp231957 于 2021-9-18 09:31 编辑

生成器不能直接访问
试一下
list(c)[0][1]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-18 09:34:49 | 显示全部楼层
错误1:c是生成器,对其索引要先转换为列表或元组
  1. >>> import os
  2. >>> c = os.walk('f:/py/')
  3. >>> d=tuple(c)
  4. >>> d[:]
复制代码


错误2:c(1),使用小括号,就会把c当作函数或类。所以提示生成器不能当作函数去调用。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-18 09:52:25 | 显示全部楼层
wp231957 发表于 2021-9-18 09:30
生成器不能直接访问
试一下
list(c)[0][1]

>>> c=os.walk('C:\\Users\\lyl\\AppData\\Local\\Programs\\Python\\Python39\\ccc')
>>> list(c)[0][1]
[]
>>> list(c)[0][0]
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    list(c)[0][0]
IndexError: list index out of range
>>> list(c)[0][2]
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    list(c)[0][2]
IndexError: list index out of range
>>> list(c)[0](2)
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    list(c)[0](2)
IndexError: list index out of range
>>>

为什么list(c)[0][1]有返回值
但是list(c)[0][2]会报错?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-18 09:53:30 | 显示全部楼层
冬雪雪冬 发表于 2021-9-18 09:34
错误1:c是生成器,对其索引要先转换为列表或元组

有没有办法让他不要返回生成器?就是直接出现我需要的值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-18 10:07:21 | 显示全部楼层
全桥整流 发表于 2021-9-18 09:53
有没有办法让他不要返回生成器?就是直接出现我需要的值

增加一步转换成列表或元组
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-18 10:22:23 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
全桥整流 发表于 2021-9-18 09:52
>>> c=os.walk('C:\%users\\lyl\\AppData\\Local\\Programs\\Python\\Python39\\ccc')
>>> list(c)[0][1 ...

先转列表,增设一个变量xx=list(c)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 10:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表