初始循环的子字符串为空字符串,每次循环运行时,新字符串添加到子字符串或再次变为空字符串?
the substring is empty string for initial loop , when everytime the loop runs , new string adds to the substring or it becomes empty string again?
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
如果我没有正确理解您的问题,那么您是在询问是否对于外部 for 循环中的每次迭代,子字符串变量在为其分配变量之前是否为空字符串。每次启动循环时(即在部分字符串分配给 sub 之后),您都会将空字符串分配给变量 substring。
我将尝试使用 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)。
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
如果我没有正确理解您的问题,那么您是在询问是否对于外部 for 循环中的每次迭代,子字符串变量在为其分配变量之前是否为空字符串。每次启动循环时(即在部分字符串分配给 sub 之后),您都会将空字符串分配给变量 substring。
我将尝试使用 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)。