相关疑难解决方法(0)

如何检查两个列表在Python中是否循环相同

例如,我有列表:

a[0] = [1, 1, 1, 0, 0]
a[1] = [1, 1, 0, 0, 1]
a[2] = [0, 1, 1, 1, 0]
# and so on
Run Code Online (Sandbox Code Playgroud)

它们似乎是不同的,但如果假设开始和结束是连接的,那么它们是圆形相同的.

问题是,我拥有的每个列表的长度为55,并且只包含三个和52个零.没有循环条件,有26,235(55选3)列表.但是,如果条件"循环"存在,则存在大量循环相同的列表

目前我通过以下方式检查循环身份:

def is_dup(a, b):
    for i in range(len(a)):
        if a == list(numpy.roll(b, i)): # shift b circularly by i
            return True
    return False
Run Code Online (Sandbox Code Playgroud)

在最坏的情况下,该功能需要55次循环移位操作.并且有26,235个列表可以相互比较.简而言之,我需要55*26,235*(26,235 - 1)/ 2 = 18,926,847,225次计算.它差不多是20吉加!

有没有什么好方法可以用更少的计算来做到这一点?或者支持循环的任何数据类型?

python algorithm

144
推荐指数
5
解决办法
1万
查看次数

Python字符串'in'运算符实现算法和时间复杂度

我正在考虑in运营商如何实施

>>> s1 = 'abcdef'
>>> s2 = 'bcd'
>>> s2 in s1
True
Run Code Online (Sandbox Code Playgroud)

在CPython中,哪个算法用于实现字符串匹配,以及时间复杂度是多少?有关于此的官方文件或维基吗?

python string algorithm cpython

26
推荐指数
1
解决办法
7472
查看次数

Python中str.replace函数的Big O表示法是什么?

str.replacePython中函数的大符号是什么?

总是O(n)吗?

str = "this is string example"
print str.replace("is", "was")
Run Code Online (Sandbox Code Playgroud)
thwas was string example
Run Code Online (Sandbox Code Playgroud)

python

8
推荐指数
2
解决办法
4003
查看次数

如何找到从 s1 到 s2 的最小可能周期偏移?

亲爱的 StackOverflowers,

我是算法的新手,我正在为以下问题苦苦挣扎:如果我有 2 个字符串s1s2,其中s2s1. 它需要找到从s1到的最小可能循环偏移s2

让我举个例子:

s1 = 'I love cookies '

s2 = 'cookies I love ' 答案在这里7

最好采用线性时间。有我失败的试验:

def find_minimum_cyclic_shift(s1, s2):

        if len(s1) != len(s2):
            return -1

        index = s2.index(s1[0])
        if (index > -1):
            if (s1==s2):
                return 0;

            #finalPosition = len(s2) - index
            #print(finalPosition, " index=",index)
            #return s2[0] == s1[finalPosition] and s1[finalPosition::]==s2[0:index]
        return index
Run Code Online (Sandbox Code Playgroud)

但它不适用于以下情况:absabsabsfabsfabsabs。而不是 4 我有 0. 因为 …

python string algorithm string-matching

3
推荐指数
1
解决办法
161
查看次数

Python - find() 函数的成本

我正在寻找一种有效的方法来检查短字符串是否在长字符串中。我在这个线程上看到了一些建议: Python effective way to check if very large string contains a substring

但是,我在那里没有看到 find() 的用法。使用 find() 函数是否昂贵?时间复杂度是多少?

我查看了 Wiki 页面,但没有找到 find() 。 https://wiki.python.org/moin/TimeComplexity

python find

1
推荐指数
1
解决办法
3815
查看次数

标签 统计

python ×5

algorithm ×3

string ×2

cpython ×1

find ×1

string-matching ×1