相同的字符串应该相等,但它们不相同

时间:2017-03-12 21:37:16

标签: python python-3.x

我花了几个小时来搜索为什么根据python的两个相同的字符串不一样。

代码的目的是找到前缀和后缀相等的最长子字符串。

我正在调试它并将值存储在内存中。它看起来一样。

而不是... == i尝试了is运算符。仍然是同样的问题。

s = 'aaaaaaa'
l = len(s)
i = 1
prefix = 0
suffix = 0
for v in s: 
    if (i < l):
        prefix = s[:i]
        suffix = s[l-i:] # idk maybe this cause some problem
        i += 1
    if (prefix.strip() == suffix.strip()):
        print(len(prefix ))

编辑:我不知道为什么但是代码开始工作了。主题可以关闭。

1 个答案:

答案 0 :(得分:0)

重新编辑您的问题, StackOverflow 的成员在没有可理解(预期;自然,合理)问题的情况下无法帮助您。一旦您有 SPECIFIC (问题的实例)问题,他们将能够帮助您,包括我,这是一个非常棒的社区。< / p>

你想要比较什么?使用 IF 语句,您可以比较 2 (或更多)值

faulty_fuses_new_variable = 'heyman!' #Added a new variable
s = 'aaaaaaa' #String type variable ---> or you can refer to it as  -  list  of characters  -     -- index [0] is the first element 
print(s[0])
    #if that's easier for you

    l = len(s) # This is an integer value variable for example for your case l = 7

    if len(s)==len(faulty_fuses_new_variable): #Checks if the lenght of the variables are the SAME 7==7 in this case.
        print ('{0} variable is the same lenght as {1} variable'.format(faulty_fuses_new_variable,s))
    #Think of {0} and {1} as place in the string that we will input our values with example :  .format(someVar,SomeOtherVar)

摘要 :您的代码中没有语法错误(IT RUNS!YAY!)但是由于您要求解决方案,因此显然存在逻辑错误。因此,需要有一个逻辑上下文问题,有许多部分你可以学习 - 如何写一个问题 - (有时很难,去过那里,做到了。不要寻找一个简单的解决方案寻找知识 - &gt; 信息)您甚至可以在那里发布您想要的问题,只是为了了解您的要求。

相关问题