V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
maloneleo88
V2EX  ›  Python

关于遍历列表,我这个错在哪了?百思不解

  •  
  •   maloneleo88 · 2021-03-22 18:19:15 +08:00 · 2036 次点击
    这是一个创建于 1124 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ha = ['273', '14549', '51511', '3', '0', '1', '1', '1', '1955548', '273', '2901', '14549', '0', '6', '1', '1', '1', '1955539', '273', '20394', '14549', '0', '2', '1', '1', '1', '1955561', '273', '14549', '2909', '3', '2', '1', '1', '1', '1955607', '273', '2915', '14549', '1', '0', '1', '1', '1', '1955592', '273', '2910', '14549', '3', '2', '1', '1', '1', '1955610', '273', '14549', '2913', '1', '3', '1', '1', '1', '1955583', '273', '14549', '24502', '2', '1', '1', '1', '1', '1955487', '273', '2900', '14549', '2', '0', '1', '1', '1', '1955473', '273', '2914', '14549', '0', '1', '1', '1', '1', '1955506', '273', '2909', '14549', '0', '0', '0', '0', '1', '1908708', '273', '14549', '24502', '2', '0', '1', '1', '1', '1908045', '273', '24502', '14549', '1', '3', '1', '1', '1', '1784091', '273', '14549', '2900', '2', '2', '0', '1', '1', '1784081', '273', '14549', '2909', '2', '0', '1', '1', '1', '1784098', '273', '2915', '14549', '2', '1', '1', '1', '1', '1784082', '273', '2910', '14549', '2', '4', '1', '1', '1', '1784076', '273', '14549', '20394', '1', '1', '0', '1', '1', '1783971', '273', '2913', '14549', '2', '3', '1', '1', '1', '1784067', '273', '14549', '2914', '1', '0', '1', '1', '1', '1784062', '273', '5953', '14549', '1', '0', '1', '1', '1', '1784055', '273', '14549', '2901', '2', '1', '1', '1', '1', '1783968', '273', '2900', '14549', '3', '1', '1', '1', '1', '1784050', '273', '14549', '2913', '0', '0', '0', '1', '1', '1784044', '273', '14549', '2915', '2', '0', '1', '1', '1', '1784040', '273', '2914', '14549', '2', '2', '0', '1', '1', '1783961', '273', '14549', '24502', '3', '2', '1', '1', '1', '1784036', '273', '2909', '14549', '2', '1', '1', '1', '1', '1784029', '273', '14549', '2901', '1', '2', '1', '1', '1', '1784025', '273', '2915', '14549', '0', '4', '1', '1', '1', '1784018']

    for i in ha[3::9]:
    if i == '0' and ha[ha.index(i) + 1] =='0':
    print(ha[int(ha.index(i))+5])

    #从第四个值开始遍历,步长 9
    如果第四个值=0,看看 4+1 是不是也=0
    如果都=0,打印 4+5 这组数字。不可以嘛?是因为设定了列表步长以后就没法得到循环以外的其他元素了吗?
    imn1
        1
    imn1  
       2021-03-22 18:26:00 +08:00
    你重新看一下 index()的用法
    应该是 i+1,不是 index(i)+1
    后面一个也是这样
    Pagliacii
        2
    Pagliacii  
       2021-03-22 18:29:04 +08:00
    index 只会返回第一个指定元素的索引,比如你代码里的 ha.index(i) 只会得到 4 这个结果
    xiri
        3
    xiri  
       2021-03-22 18:29:12 +08:00
    如果 i 等于 0,不管你遍历到的是哪一个 0,ha.index(i)永远返回 4 ( list.index 是返回列表中第一个等于该值的元素的索引)
    Pagliacii
        4
    Pagliacii  
       2021-03-22 18:29:43 +08:00
    @Pagliacii #2 如果你想同时遍历元素和获取索引,可以用 enumerate
    maloneleo88
        5
    maloneleo88  
    OP
       2021-03-22 18:33:03 +08:00
    @Pagliacii enumerate 用了 获取的索引是重新排序的,0,1,2,3,4,5 这样子

    @imn1 那就知道报错字符串+数字了
    maloneleo88
        6
    maloneleo88  
    OP
       2021-03-22 18:35:10 +08:00 via Android
    @maloneleo88 打错字,直接报错,此时变量 i 是字符串直接加数字报错
    Pagliacii
        7
    Pagliacii  
       2021-03-22 18:35:32 +08:00
    @maloneleo88 #5 那就直接 for i in range(3, len(ha), 9)
    imn1
        8
    imn1  
       2021-03-22 18:40:37 +08:00
    @maloneleo88 #5
    这程序看得晕,i 一般表示下标,要同时使用下标和值,就 i,v 对应 enumerate 好了
    ch2
        9
    ch2  
       2021-03-22 18:41:42 +08:00
    有重复元素,用 index 会坑的
    maloneleo88
        10
    maloneleo88  
    OP
       2021-03-22 18:43:36 +08:00
    @Pagliacii
    @imn1

    明白了明白, 真笨啊。 先拿到索引,ha[i] 就可以了。。 谢谢
    maloneleo88
        11
    maloneleo88  
    OP
       2021-03-22 18:44:43 +08:00
    @ch2 是啊,新手不知道。 以前都是只有一个元素,很好用。不明白为什么不能直接用 index 循环,这样多智能
    biglazycat
        12
    biglazycat  
       2021-03-23 06:36:56 +08:00
    result = list(enumerate(ha))

    for i in result[3::9]:
    if ha[i[0]] == '0' and ha[i[0] + 1] == '0':
    print(f"ha[{i[0] + 5}]: {ha[i[0] + 5]}")
    princelai
        13
    princelai  
       2021-03-24 10:57:19 +08:00
    reshape 一下维度做起来不是更方便吗

    ```
    ha_arr = np.array(ha)
    ha_arr = ha_arr.reshape(-1,9).astype(int)
    np.where((ha_arr[:,4]==0)&(ha_arr[:,5]==0),ha_arr[:,8],99999)
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   914 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:36 · PVG 04:36 · LAX 13:36 · JFK 16:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.