Python索引-我不理解这段代码是如何工作的

Python索引-我不理解这段代码是如何工作的,python,python-3.x,Python,Python 3.x,我不明白的是,如果我在按字母顺序查找最长的子字符串,那么为字符串s=“azcbobeghakl”编制索引将如何正确地给出“beggh”的答案。这个代码是如何告诉计算机“beggh”是按字母顺序排列的?我以为索引只是给字符串中的每个字母加一个数字,直到字符串结束?我对代码进行了一些调整,它确实起了作用,但我并不真正理解代码最初是如何工作的 s = "azcbobobegghakl" longest = s[0] current = s[0] for c in s[1:]: if c >

我不明白的是,如果我在按字母顺序查找最长的子字符串,那么为字符串s=“azcbobeghakl”编制索引将如何正确地给出“beggh”的答案。这个代码是如何告诉计算机“beggh”是按字母顺序排列的?我以为索引只是给字符串中的每个字母加一个数字,直到字符串结束?我对代码进行了一些调整,它确实起了作用,但我并不真正理解代码最初是如何工作的

s = "azcbobobegghakl"
longest = s[0]
current = s[0]
for c in s[1:]:
    if c >= current[-1]:
        current += c
    else:
        if len(current) > len(longest):
            longest = current
        current = c
print "Longest substring in alphabetical order is:", longest

=
根据两个字符的字典顺序进行比较;表情

if c >= current[-1]:
测试
c
(单个字符)是否与
current[-1]
(另一个单个字符)相同或“大于”。
a
小于
b
,因为在Unicode标准中,字符
a
位于
b
之前:

>>> 'a' > 'b'
False
>>> 'a' < 'b'
True
>'a'>'b'
假的
>>>“a”<“b”
真的

因此,如果字符在字母表中比当前的最后一个字符“晚”出现,则只会将其添加到当前的字符中。如果不是,则会测试到目前为止的序列的长度,并从
c

=
开始一个新的子字符串,该子字符串按字母顺序比较两个字符;expr休会

if c >= current[-1]:
测试
c
(单个字符)是否与
current[-1]
(另一个单个字符)相同或“大于”。
a
小于
b
,因为在Unicode标准中,字符
a
位于
b
之前:

>>> 'a' > 'b'
False
>>> 'a' < 'b'
True
>'a'>'b'
假的
>>>“a”<“b”
真的

因此,如果字符在字母表中比当前的最后一个字符“晚”出现,则只会将其添加到当前的字符中。如果不是,则会测试到目前为止的序列长度,并从表达式
c
开始一个新的子字符串
c>=current[-1]
按字典顺序将字符串中的每个字符与其前一个字符进行比较。更简单的是:

>>> 'a' >= 'a'
True
>>> 'b' >= 'a'
True
>>> 'b' >= 'c'
False
>>> 'z' >= 'a'
True

因此,代码所做的就是累积字符运行,每个字符都是最后一个字符,并保留最长的字符。

表达式
c>=current[-1]
按字典顺序将字符串中的每个字符与其前一个字符进行比较。更简单地说:

>>> 'a' >= 'a'
True
>>> 'b' >= 'a'
True
>>> 'b' >= 'c'
False
>>> 'z' >= 'a'
True
s = "azcbobobegghakl"

# initialize vars to first letter in string
longest = s[0]
current = s[0]

# start iterating through rest of string
for c in s[1:]:

    # c is the letter of current iteration and current[-1]
    # is the last letter of the current string. Every character 
    # has an ASCII value so this is checking the character's 
    # numerical values to see if they are in alphabetical order (a<b<c etc.)
    if c >= current[-1]:

        # so if c is bigger than current[-1] (meaning it comes
        # after in the alphabet, then we want to add it to current
        current += c
    else:
        # if c was smaller then we want to check if the alphabetic
        # string we just read is longer than the previous longest
        if len(current) > len(longest):
            longest = current

        # start the current string over beginning with the letter in c
        current = c
print "Longest substring in alphabetical order is:", longest
因此,代码所做的就是累积字符的运行,每个字符都是最后一个字符,并保留看到的最长字符。

s=“azcbobeghakl”
s = "azcbobobegghakl"

# initialize vars to first letter in string
longest = s[0]
current = s[0]

# start iterating through rest of string
for c in s[1:]:

    # c is the letter of current iteration and current[-1]
    # is the last letter of the current string. Every character 
    # has an ASCII value so this is checking the character's 
    # numerical values to see if they are in alphabetical order (a<b<c etc.)
    if c >= current[-1]:

        # so if c is bigger than current[-1] (meaning it comes
        # after in the alphabet, then we want to add it to current
        current += c
    else:
        # if c was smaller then we want to check if the alphabetic
        # string we just read is longer than the previous longest
        if len(current) > len(longest):
            longest = current

        # start the current string over beginning with the letter in c
        current = c
print "Longest substring in alphabetical order is:", longest
#将变量初始化为字符串中的第一个字母 最长=s[0] 电流=s[0] #开始遍历字符串的其余部分 对于s[1:]中的c: #c是当前迭代和当前[-1]的字母 #是当前字符串的最后一个字母。每个字符 #具有ASCII值,因此这是检查字符的 #数值,以查看它们是否按字母顺序排列(长度): 最长=电流 #将当前字符串从c中的字母开始 电流=c 打印“按字母顺序排列的最长子字符串为:”,最长
s=“azcbobegbegghakl”
#将变量初始化为字符串中的第一个字母
最长=s[0]
电流=s[0]
#开始遍历字符串的其余部分
对于s[1:]中的c:
#c是当前迭代和当前[-1]的字母
#是当前字符串的最后一个字母。每个字符
#具有ASCII值,因此这是检查字符的
#数值,以查看它们是否按字母顺序排列(长度):
最长=电流
#将当前字符串从c中的字母开始
电流=c
打印“按字母顺序排列的最长子字符串为:”,最长

那么python会自动执行此操作,而无需调用任何函数?什么初始化(?)分配ASCII值并进行比较的代码?ASCII代表美国信息交换标准代码,大约在50多年前,他们就决定了每个字符的值。您可以在此处阅读有关ASCII的内容:。操作系统和编译器的工作是将位值转换为各自的字母。请看这里:So pyth启用此选项是否自动执行,而无需调用任何函数?初始化什么(?)分配ASCII值并进行比较的代码?ASCII代表美国信息交换标准代码,大约在50多年前,他们就决定了每个字符的值。您可以在此处阅读有关ASCII的内容:。操作系统和编译器的工作是将位值转换为各自的字母。请看这里:因为我疯了,这似乎是一个有趣的一对一的行,具有等效的big-O(如果你能使用简单的解决方案,这太容易了):
来自itertools导入链,groupby
来自operator import itemgetter,le
,define
s
,然后一行是:
longest=max('.join(chain(next(g),map(itemgetter(1),g)))对于按顺序排列的,g在groupby中(zip(chain(“”),s[:-1]),s,key=lambda p:le(*p))(如果按顺序排列),key=len)
。这并不是真的想有所帮助,只是想炫耀一下你可以用
itertools
操作符
:-)做什么可怕的事情因为我疯了,这看起来像是一个有趣的一对一的行,相当于big-O(如果你能使用简单的解决方案,这太容易了):
来自itertools导入链,groupby
来自operator import itemgetter,le
,define
s
,然后一行是:
longest=max('.join(chain(next(g),map(itemgetter(1),g)))对于按顺序排列的,g在groupby中(zip(chain(“”),s[:-1]),s,key=lambda p:le(*p))(如果按顺序排列),key=len)
。这并不是真的想有所帮助,只是想炫耀一下你可以用
itertools
操作符
:-)做什么可怕的事情