社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Alex R

Alex R 最近回复了
3 年前
回复了 Alex R 创建的主题 » 在Python中查找n个元素相等的索引

如果当前元素等于上一个元素,则可以迭代列表并增加计数器。如果你的柜台 n ,您可以轻松计算指数:

def find(lst, n: int):
    counter = 1
    previous = None

    for index, element in enumerate(lst):
        if element == previous:
            counter += 1
        else:
            counter = 1

        if counter == n:
            return index - n + 1

        previous = element

    return -1

输出:

A = [False, False, True, True, True, False, True, True, True]
print(find(A, 3))

-> 2

哈希表!

需要更多的信息,但如果你已经有了数据,你可以把它分类为这些可以索引和减少整个过程时间!

当然,最初计算条目的去向需要时间,但是。

有点像

def hash(astring, tablesize):
sum = 0
for pos in range(len(astring)):
    sum = sum + ord(astring[pos])

return sum%tablesize