简体   繁体   English

python 中的字符串字符之间的不等式

[英]Inequality between string characters in python

I am new to StackOverflow so forgive me if I did any formatting wrong.我是 StackOverflow 的新手,所以如果我做错任何格式,请原谅我。

I am trying to extract the largest substring (with variable name 'longest_string') which follows an alphabetical order (ie a,b,x,y,z is valid, but a,c,b is not) from a string (with variable name 's').我正在尝试从字符串(带有变量名称's')。

What I tried to do is to create a loop which looped over every single character in ' s ', and assigned the first character of string s to the variable ' current_value ' and add it into a list called ' strings '.我试图做的是创建一个循环,循环遍历' s '中的每个字符,并将字符串s的第一个字符分配给变量' current_value '并将其添加到一个名为' strings '的列表中。 Then, I compare the subsequent character to see if that character is larger than or equal to the current_value .然后,我比较下一个字符以查看该字符是否大于或等于current_value If it is, then I add it to ' strings ', and if it isn't, I assigned the initial ' strings ' list to the ' longest_string ' list and refreshed the variable ' current_value ' and ' strings '.如果是,那么我将它添加到“ strings ”,如果不是,我将初始的“ strings ”列表分配给“ longest_string ”列表并刷新变量“ current_value ”和“ strings ”。 Each time the subsequent character is smaller than the ' current_value ', the length of current ' strings ' is compared with the ' longest_string ' and the longest one is assigned as ' longest_string '.每次后续字符小于“ current_value ”时,将当前“ strings ”的长度与“ longest_string ”进行比较,最长的一个被指定为“ longest_string ”。

What I understand is that Python assumes that the first alphabet in the 26 English character to be the smallest (ie 'a' > 'z' will return False).我的理解是 Python 假设 26 个英文字符中的第一个字母是最小的(即 'a' > 'z' 将返回 False)。 However, when I run the below code:但是,当我运行以下代码时:

s = 'zcbobobegghakl'
longest_string = None
current_value = None
strings = []

for i in s:
    if not current_value:
        current_value = i
        strings.append(i)
        continue

    if i >= current_value:
        strings.append(i)

    if i < current_value:
        if not longest_string or len(longest_string) < len(strings):
            longest_string = strings
        current_value = i
        strings = []
        
print(longest_string)

I get this output instead:我得到这个 output 代替:

['o', 'b', 'o', 'b', 'e', 'g', 'g', 'h']

Why is it that the character 'b' got assigned into the same string as 'o' when clearly 'b' > 'o' will return False and thus make the 2nd IF statement False, wouldn't it not execute the 2nd IF statement in the first place?为什么字符 'b' 被分配到与 'o' 相同的字符串中,而显然 'b' > 'o' 将返回 False 从而使第二个 IF 语句为 False,它不会不执行第二个 IF 语句首先?

Try adding this into the beginnig of the loop.尝试将其添加到循环的开头。 Maybe this will give you some insight?也许这会给你一些见解?

...
for i in s:
    print(i, current_value, strings)
    if not current_value:
...

Why is it that the character 'b' got assigned into the same string as 'o'为什么字符'b'被分配到与'o'相同的字符串中

current_value only decreases, and after you encounter first b accumulation continues until a is found current_value只会减少,遇到第一个b后,会继续累积,直到找到a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM