在初始循环中,子串是空字符串,当每次循环运行时,新的字符串会添加到子串中,还是又变成空字符串?

问题描述 投票:0回答:1
def merge_the_tools(string, k):
strings_list = []                     
for i in range(0,len(string),k):               # incrementing the loop by k
    sub = (string[i:i+k])
    substring=''                           # empty substrings
    for x in sub:
        if x not in substring:          # removing the duplicates
            substring = substring + x
    strings_list.append(substring)            # adding it into empty list
print(*strings_list, sep='\n')      # printing the list

/这段代码根据变量'k'将一个字符串分解成子串,然后以一种有序的方式检查并删除子串中的重复部分,并打印新的子串。

输入:merge_the_tools('AABCAAADA', 3) 输出:AB CA AD。

python python-3.x string jupyter-notebook substr
1个回答
0
投票

如果我对你的问题理解正确的话,你问的是在你的外层for-loop中的每一次迭代中,substring变量是否会在赋值给它之前是一个空的sting。是这样的,因为每次开始循环时,你都会将空字符串分配给变量substring(即在字符串的一部分分配给sub后)。

我将尝试使用string = 'AABCAAADA',k = 3来逐步完成你的函数(我根据例子的需要复制了多次代码,并在代码中添加了注释)。

merge_the_tools('AABCAAADA', 3) #function call will trigger execution of function
strings_list = []                     
for i in range(0,len('AABCAAADA'), 3): #outer loop
# iteration 1: i = 0:
    sub = (string[0:3]) # = 'AAB'
    substring=''
    for x in sub: #inner loop
        # iteration 1.1: x = 'A'
        if 'A' not in substring: #substring is empty
            substring = '' + 'A' # substring = 'A' then go to next x in inner loop
        # iteration 1.2: x = 'A'
        if 'A' not in substring: #substring = 'A' -> condition not fullfilled, go to next x in inner loop
        # iteration 1.3: x = 'B'
        if 'B' not in substring: #substring = 'A'
        substring = 'A' + 'B' # substring = 'AB' -> last x in inner loop -> continue outer loop
    strings_list.append('AB') # strings_list = ['AB'], end of outer loop -> continue with next i of outer loop
# iteration 2: i = 3:
    sub = (string[3:6]) # = 'CAA'
    substring='' #here the empty string is reassigned to substring! 
    for x in sub: #inner loop
        # iteration 2.1: x = 'C'
        if 'C' not in substring: #substring is empty
            substring = '' + 'C' # substring = 'C' then go to next x in inner loop
        # iteration 2.2: x = 'A'
        if 'A' not in substring: #substring = 'C'
        substring = 'C' + 'A' # substring = 'CA' -> go to next x in inner loop
        # iteration 2.3: x = 'A'
        if 'A' not in substring: #substring = 'CA' -> condition not fullfilled, last x in inner loop -> continue outer loop
    strings_list.append('CA') # strings_list = ['AB', 'CA'], end of outer loop -> continue with next i of outer loop
# iteration 3: i = 6:
    sub = (string[6:9]) # = 'ADA'
    substring='' #here the empty string is reassigned to substring! 
    for x in sub: #inner loop
        # iteration 3.1: x = 'A'
        if 'A' not in substring: #substring is empty
            substring = '' + 'A' # substring = 'A' then go to next x in inner loop
        # iteration 3.2: x = 'D'
        if 'D' not in substring: #substring = 'A'
        substring = 'A' + 'D' # substring = 'AD' -> go to next x in inner loop
        # iteration 3.3: x = 'A'
        if 'A' not in substring: #substring = 'AD' -> condition not fullfilled, last x in inner loop -> continue outer loop
    strings_list.append('AD') # strings_list = ['AB', 'CA', 'AD'], end of outer loop -> last i of outer loop -> exit loop

print(*strings_list, sep='\n') #output :AB CA AD

希望这能让事情变得更清楚一些。另外如果你对循环的概念不熟悉,有很多好的教程(google一下就知道了)。

© www.soinside.com 2019 - 2024. All rights reserved.