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
oneTimeElastic
V2EX  ›  Python

怎么样找到一个列表中的连续数的第一个 index?

  •  
  •   oneTimeElastic · 2019-05-02 12:32:01 +08:00 · 1618 次点击
    这是一个创建于 1821 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如有一个列表[1,2,3,3,3,9,9,9], 因为 3 和 9 都连续出现了 3 次,那输出应该是其中比较大的,也就是 9 的连续数的第一个 index,即 5. 而如果列表是[1,9,9,3,2,9,9,9],因为 9 最后出现了三次,那输出应该是 index 5.
    我一开始的想法是 itertools 的 groupby, 但是应该怎么输出第一个 index 啊。。希望有人帮忙解答,谢谢!
    8 条回复    2019-05-05 01:03:44 +08:00
    Lax
        1
    Lax  
       2019-05-02 12:36:44 +08:00
    到底要第一个 index 还是最后一个 index ?

    去掉第一个元素,然后对应位置相减试试:
    [1,2,3,3,3,9,9,9]
    [2,3,3,3,9,9,9]
    noqwerty
        2
    noqwerty  
       2019-05-02 12:45:26 +08:00 via Android
    笨方法就是建两个列表 obs_num 和 obs_count,前者是观测到的数字,后者是这个数字连续出现的次数,然后输出 obs_count 最大时 obs_num 的最大值。
    whusnoopy
        3
    whusnoopy  
       2019-05-02 12:45:29 +08:00
    完全没看懂你的问题是什么,我猜要求是

    1. 连续次数最多的数在连续最多次那一段的第一个数的下标
    2. 在 1 的基础上,如果有多个数字出现了相同的最多连续次数,选最大的那个数符合 1 的输出
    3. 在 2 的基础上,如果有多个相同数字出现了最多连续次数且都是最大,选第一次连续段符合 1 的输出

    如果是我的这个猜测,那么

    ```python
    # coding: utf8

    list1 = [1, 2, 3, 3, 3, 9, 9, 9]
    list2 = [1, 9, 9, 3, 2, 9, 9, 9]


    def first_index(input_list):
    fi = [0, 0, -1] # 已知符合条件的数字,最大连续次数,首次下标
    curr = [None, 0, -1]

    for idx, num in enumerate(input_list):
    if num != curr[0]:
    curr = [num, 1, idx]
    else:
    curr[1] += 1

    if curr[1] > fi[1]:
    fi[0] = curr[0]
    fi[1] = curr[1]
    fi[2] = curr[2]
    elif curr[1] == fi[1]:
    if curr[0] > fi[0]:
    fi[0] = curr[0]
    fi[1] = curr[1]
    fi[2] = curr[2]

    return fi[2]


    print first_index(list1)
    print first_index(list2)

    ```
    oneTimeElastic
        4
    oneTimeElastic  
    OP
       2019-05-02 12:46:18 +08:00
    是要一组连续数的第一个数的 index
    能讲讲什么叫对应位置相减吗
    oneTimeElastic
        5
    oneTimeElastic  
    OP
       2019-05-02 12:54:36 +08:00
    @whusnoopy 嗯就是这个要求 真是惭愧,语文表达都成问题
    这个办法很好,我一开始用 dictionary 记录 index 和次数,但没用 enumerate 而使用 while i < len(ls) 然后再遇到重复 i += 1 这样的,结果遇到了超了 list 的 index 哎
    Northxw
        6
    Northxw  
       2019-05-03 12:12:43 +08:00
    这应该是道算法题(不知道是不是 leetcode 上面的....)
    necomancer
        7
    necomancer  
       2019-05-05 01:02:59 +08:00
    l = [1,2,3,3,3,9,9,9]
    l = [1,9,9,9,3,2,9,9,9]
    counts = [[1,0,l[0]]]
    i = 0
    for idx, item in enumerate(zip(l[:-1], l[1:])):
    prev, nxt = item
    if prev == nxt:
    counts[i][0] += 1
    else:
    counts.append([1,nxt,idx+1])
    i += 1

    print(counts, l)
    print(sorted(counts, key=lambda x:(x[0],x[1],-x[2]), reverse=True)[0])
    # 按出现次数、元素本身大小和元素序号(逆序)排序
    print(sorted(counts, key=lambda x:(x[0],x[1],-x[2]), reverse=True)[0])
    # 按出现次数、元素本身大小和元素序号排序
    necomancer
        8
    necomancer  
       2019-05-05 01:03:44 +08:00
    输出:

    [[1, 0, 1], [3, 9, 1], [1, 3, 4], [1, 2, 5], [3, 9, 6]] [1, 9, 9, 9, 3, 2, 9, 9, 9]
    [3, 9, 1]
    [3, 9, 6
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2865 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:28 · PVG 19:28 · LAX 04:28 · JFK 07:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.