DNA 搜索序列正则表达式中存在多个不匹配
我编写了这个野蛮的脚本来创建字符串的排列,其中在字符串中所有可能的位置组合中包含 n 个(最多 n=4)个 $。我最终将 .replace('$','(\\w)')
用于 dna 搜索序列中的不匹配。由于我编写脚本的方式,某些排列的 $ 数量少于请求的数量。然后我编写了一个脚本来删除它们,但它似乎没有效果,并且每次运行删除脚本时,它都会删除更多不需要的排列。在下面粘贴的代码中,您将看到我使用具有 4 个不匹配的简单序列来测试该函数。然后,我运行一系列删除脚本,计算每次删除的表达式数量...根据我的经验,删除所有少于 4 个通配符 $ 的表达式大约需要 8 次。我对此有几个问题:
是否有用于搜索“n”个不匹配项的内置函数?也许甚至在biopython中?到目前为止,我已经看到了 Paul_McGuire_regex 函数:
搜索允许出现 1 的字符串字符串任意位置不匹配,
这似乎只会产生 1 个不匹配。我必须承认,我并不完全理解该页面上其余函数中的所有代码,因为我是一个非常新的编码员。- 需要多次迭代 Paul_McGuire_regex 函数吗?
最让我困惑的是,为什么删除脚本第一次不能 100% 工作?
感谢您提供的任何帮助!
def Mismatch(Search,n):
List = []
SearchL = list(Search)
if n > 4:
return("Error: Maximum of 4 mismatches")
for i in range(0,len(Search)):
if n == 1:
SearchL_i = list(Search)
SearchL_i[i] = '$'
List.append(''.join(SearchL_i))
if n > 1:
for j in range (0,len(Search)):
if n == 2:
SearchL_j = list(Search)
SearchL_j[i] = '$'
SearchL_j[j] = '$'
List.append(''.join(SearchL_j))
if n > 2:
for k in range(0,len(Search)):
if n == 3:
SearchL_k = list(Search)
SearchL_k[i] = '$'
SearchL_k[j] = '$'
SearchL_k[k] = '$'
List.append(''.join(SearchL_k))
if n > 3:
for l in range(0,len(Search)):
if n ==4:
SearchL_l = list(Search)
SearchL_l[i] = '$'
SearchL_l[j] = '$'
SearchL_l[k] = '$'
SearchL_l[l] = '$'
List.append(''.join(SearchL_l))
counter=0
for el in List:
if el.count('$') < n:
counter+=1
List.remove(el)
return(List)
List_RE = Mismatch('abcde',4)
counter = 0
for el in List_RE:
if el.count('$') < 4:
List_RE.remove(el)
counter+=1
print("Filter2="+str(counter))
I have written this barbaric script to create permutations of a string of characters that contain n (up to n=4) $'s in all possible combinations of positions within the string. I will eventually .replace('$','(\\w)')
to use for mismatches in a dna search sequence. Because of the way I wrote the script, some of the permutations have less than the requested number of $'s. I then wrote a script to remove them, but it doesn't seem to be effective, and each time I run the removal script, it removes more of the unwanted permutations. In the code pasted below, you will see that I test the function with a simple sequence with 4 mismatches. I then run a series of removal scripts that count how many expressions are removed each time...in my experience, it takes about 8 times to remove all expressions with less than 4 wild-card $'s. I have a couple questions about this:
Is there a built in function for searches with 'n' mismatches? Maybe even in biopython? So far, I've seen the Paul_McGuire_regex function:
Search for string allowing for one mismatch in any location of the string,
which seems only to generate 1 mismatch. I must admit, I don't fully understand all of the code in the remainining functions on that page, as I am a very new coder.Since I see this as a good exercise for me, is there a better way to write this entire script?...Can I iterate Paul_McGuire_regex function as many times as I need?
Most perplexing to me, why won't the removal script work 100% the first time?
Thanks for any help you can provide!
def Mismatch(Search,n):
List = []
SearchL = list(Search)
if n > 4:
return("Error: Maximum of 4 mismatches")
for i in range(0,len(Search)):
if n == 1:
SearchL_i = list(Search)
SearchL_i[i] = '
List.append(''.join(SearchL_i))
if n > 1:
for j in range (0,len(Search)):
if n == 2:
SearchL_j = list(Search)
SearchL_j[i] = '
SearchL_j[j] = '
List.append(''.join(SearchL_j))
if n > 2:
for k in range(0,len(Search)):
if n == 3:
SearchL_k = list(Search)
SearchL_k[i] = '
SearchL_k[j] = '
SearchL_k[k] = '
List.append(''.join(SearchL_k))
if n > 3:
for l in range(0,len(Search)):
if n ==4:
SearchL_l = list(Search)
SearchL_l[i] = '
SearchL_l[j] = '
SearchL_l[k] = '
SearchL_l[l] = '
List.append(''.join(SearchL_l))
counter=0
for el in List:
if el.count('
) < n:
counter+=1
List.remove(el)
return(List)
List_RE = Mismatch('abcde',4)
counter = 0
for el in List_RE:
if el.count('
) < 4:
List_RE.remove(el)
counter+=1
print("Filter2="+str(counter))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以通过回答问题 1 来消除问题 2 和 3,但是理解问题 3 很重要,所以我将首先这样做,然后展示如何完全避免它:
问题 3
至于问题 3,这是因为当你循环时python 中的列表并在循环内对其进行更改,您循环的列表会发生变化。
来自 有关控制流的 python 文档(针对语句部分):
假设您的列表是
[a,b,c,d]
,然后使用for el in List
循环遍历它。假设
el
当前是a
并且您执行List.remove(el)
。现在,您的列表是
[b,c,d]
。然而,迭代器指向列表中的第二个元素(因为它已经完成了第一个元素),现在是c
。本质上,您已经跳过了
b
。所以问题是你正在修改你正在迭代的列表。有几种方法可以解决此问题:如果您的
List
复制成本不高,您可以制作一个副本。因此,迭代List[:]
但从List
中删除。但假设一直复制
List
的成本很高。然后你要做的就是向后迭代它。请注意下面的
reversed
:在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
List.append( ''.join(str) ) # convert back to string return List让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
, '$b$e', '$b$d ) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
List.append( ''.join(str) ) # convert back to string return List让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
, '$bc$', 'a$$e', 'a$d ) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
List.append( ''.join(str) ) # convert back to string return List让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
, 'a$c$', 'ab$ ) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
List.append( ''.join(str) ) # convert back to string return List让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
] Mismatch('abcde',4) # note, the code you had made lots of duplicates. ['$$e', '$$d ) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
List.append( ''.join(str) ) # convert back to string return List让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
, '$c$', '$b$ ) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
List.append( ''.join(str) ) # convert back to string return List让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
, 'a$$'] ) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
List.append( ''.join(str) ) # convert back to string return List让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
) < n: counter+=1 List.remove(el) return(List)在上面的示例中,假设我们向后迭代
List
。迭代器从
d
开始,然后转到c
。假设我们删除
c
,因此List=[a,b,d]
。由于迭代器向后,它现在指向元素
b
,因此我们没有跳过任何内容。基本上,这可以避免修改列表中尚未迭代的位。
问题 1 和 2 2
如果我正确理解你的问题,你基本上想从
m
个位置中选择n
,其中m
是字符串的长度(< code>abcde),并在每个n
位置放置一个“$”。在这种情况下,您可以使用 itertools 模块来执行此操作。
让我们看看它是如何工作的:
Search
字符串转换为一个列表,以便可以对其进行迭代,创建空的List
来保存结果。idxs = itertools.combinations(range(len(SearchL)),n)
表示“在集合[0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
。尝试一下
明白我的意思。
idxs
的每个元素都是从 0 到len(SearchL)-1
的n
个索引的元组(例如(0,1, 元组中的每个
i
的第 i 个字符替换为“$”。列表
中。 例子:
We can do away with questions 2 and 3 by answering question 1, but understanding question 3 is important so I'll do that first and then show how you can avoid it entirely:
Question 3
As to question 3, it's because when you loop over a list in python and make changes to it within the loop, the list that you loop over changes.
From the python docs on control flow (for statement section):
Say your list is
[a,b,c,d]
and you loop through it withfor el in List
.Say
el
is currentlya
and you doList.remove(el)
.Now, your list is
[b,c,d]
. However, the iterator points to the second element in the list (since it's done the first), which is nowc
.In essence, you've skipped
b
. So the problem is that you are modifying the list you are iterating over.There are a few ways to fix this: if your
List
is not expensive to duplicate, you could make a copy. So iterate overList[:]
but remove fromList
.But suppose it's expensive to make copies of
List
all the time.Then what you do is iterate over it backwards. Note the
reversed
below:In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
List.append( ''.join(str) ) # convert back to string return ListLet's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
, '$b$e', '$b$d ) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with 'Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
List.append( ''.join(str) ) # convert back to string return ListLet's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
, '$bc$', 'a$$e', 'a$d ) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with 'Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
List.append( ''.join(str) ) # convert back to string return ListLet's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
, 'a$c$', 'ab$ ) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with 'Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
List.append( ''.join(str) ) # convert back to string return ListLet's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
] Mismatch('abcde',4) # note, the code you had made lots of duplicates. ['$$e', '$$d ) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with 'Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
List.append( ''.join(str) ) # convert back to string return ListLet's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
, '$c$', '$b$ ) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with 'Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
List.append( ''.join(str) ) # convert back to string return ListLet's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
, 'a$$'] ) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with 'Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
List.append( ''.join(str) ) # convert back to string return ListLet's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example:
) < n: counter+=1 List.remove(el) return(List)In the example above, suppose we iterate backwards over
List
.The iterator starts at
d
, and then goes toc
.Suppose we remove
c
, so thatList=[a,b,d]
.Since the iterator is going backwards, it now points to element
b
, so we haven't skipped anything.Basically, this avoids modifying bits of the list you have yet to iterate over.
Questions 1 & 2
If I understand your question correctly, you basically want to choose
n
out ofm
positions, wherem
is the length of the string (abcde
), and place a '$' in each of thesen
positions.In that case, you can use the
itertools
module to do that.Let's look at how this works:
Search
string into a list so it can be iterated over, create emptyList
to hold results.idxs = itertools.combinations(range(len(SearchL)),n)
says "find all subsets of length n in the set[0,1,2,3,...,length-of-search-string -1]
.Try
to see what I mean.
idxs
is a tuple ofn
indices from 0 tolen(SearchL)-1
(e.g.(0,1,2,4)
. Replace the i'th character ofSearchL
with a '$' for eachi
in the tuple.List
.As an example: