DNA 搜索序列正则表达式中存在多个不匹配

发布于 2024-12-21 11:30:50 字数 2503 浏览 2 评论 0原文

我编写了这个野蛮的脚本来创建字符串的排列,其中在字符串中所有可能的位置组合中包含 n 个(最多 n=4)个 $。我最终将 .replace('$','(\\w)') 用于 dna 搜索序列中的不匹配。由于我编写脚本的方式,某些排列的 $ 数量少于请求的数量。然后我编写了一个脚本来删除它们,但它似乎没有效果,并且每次运行删除脚本时,它都会删除更多不需要的排列。在下面粘贴的代码中,您将看到我使用具有 4 个不匹配的简单序列来测试该函数。然后,我运行一系列删除脚本,计算每次删除的表达式数量...根据我的经验,删除所有少于 4 个通配符 $ 的表达式大约需要 8 次。我对此有几个问题:

  1. 是否有用于搜索“n”个不匹配项的内置函数?也许甚至在biopython中?到目前为止,我已经看到了 Paul_McGuire_regex 函数:
    搜索允许出现 1 的字符串字符串任意位置不匹配,
    这似乎只会产生 1 个不匹配。我必须承认,我并不完全理解该页面上其余函数中的所有代码,因为我是一个非常新的编码员。

  2. 需要多次迭代 Paul_McGuire_regex 函数吗?

  3. 最让我困惑的是,为什么删除脚本第一次不能 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:

  1. 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.

  2. 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?

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-12-28 11:30:50

我们可以通过回答问题 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

for el in reversed(List):
    if el.count('

在上面的示例中,假设我们向后迭代List
迭代器从d 开始,然后转到c
假设我们删除c,因此List=[a,b,d]
由于迭代器向后,它现在指向元素b,因此我们没有跳过任何内容。

基本上,这可以避免修改列表中尚未迭代的位。

问题 1 和 2 2

如果我正确理解你的问题,你基本上想从 m 个位置中选择 n ,其中 m 是字符串的长度(< code>abcde),并在每个 n 位置放置一个“$”。

在这种情况下,您可以使用 itertools 模块来执行此操作。

import itertools
def Mismatch(Search,n):
    SearchL = list(Search)
    List     = [] # hold output
    # print list of indices to replace with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    idxs = itertools.combinations(range(5),4)
    对于 idxs 中的 idx: 
        打印IDX
    

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

Mismatch('abcde',3)
['$$de', '$c$e', '$cd
) < 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 模块来执行此操作。


让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    
    

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:



    idxs = itertools.combinations(range(len(SearchL)),n)        
    # for each combination `idx` in idxs, replace str[idx] with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    
    

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:


) < 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 模块来执行此操作。


让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    
    

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:


:
    for idx in idxs:
        str = SearchL[:] # make a copy
        for i in idx:
            str[i]='

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    
    

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:


) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

List.append( ''.join(str) ) # convert back to string return List

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

, '$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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

List.append( ''.join(str) ) # convert back to string return List

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

, '$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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

List.append( ''.join(str) ) # convert back to string return List

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

, '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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

List.append( ''.join(str) ) # convert back to string return List

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

] 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

List.append( ''.join(str) ) # convert back to string return List

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

, '$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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

List.append( ''.join(str) ) # convert back to string return List

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

, '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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

: for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

List.append( ''.join(str) ) # convert back to string return List

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

) < 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 模块来执行此操作。

让我们看看它是如何工作的:

  1. Search 字符串转换为一个列表,以便可以对其进行迭代,创建空的 List 来保存结果。
  2. idxs = itertools.combinations(range(len(SearchL)),n) 表示“在集合 [0,1,2,3,.”中查找长度为 n 的所有子集。 ...,搜索字符串长度-1]
    尝试一下

    明白我的意思。

  3. idxs 的每个元素都是从 0 到 len(SearchL)-1n 个索引的元组(例如 (0,1, 元组中的每个 i 的第 i 个字符替换为“$”。
  4. 2,4)。将 并将其添加到列表

。 例子:

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):

It is not safe to modify the sequence being iterated over in the loop
(this can only happen for mutable sequence types, such as lists).

Say your list is [a,b,c,d] and you loop through it with for el in List.
Say el is currently a and you do List.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 now c.
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 over List[:] but remove from List.

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:

for el in reversed(List):
    if el.count('

In the example above, suppose we iterate backwards over List.
The iterator starts at d, and then goes to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

import itertools
def Mismatch(Search,n):
    SearchL = list(Search)
    List     = [] # hold output
    # print list of indices to replace with '

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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

    idxs = itertools.combinations(range(5),4)
    for idx in idxs: 
        print idx
    

    to see what I mean.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

Mismatch('abcde',3)
['$$de', '$c$e', '$cd
) < 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.


Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.


Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

List.append( ''.join(str) ) # convert back to string return List

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

List.append( ''.join(str) ) # convert back to string return List

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

List.append( ''.join(str) ) # convert back to string return List

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

List.append( ''.join(str) ) # convert back to string return List

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

List.append( ''.join(str) ) # convert back to string return List

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

List.append( ''.join(str) ) # convert back to string return List

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

List.append( ''.join(str) ) # convert back to string return List

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to 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 to c.
Suppose we remove c, so that List=[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 of m positions, where m is the length of the string (abcde), and place a '$' in each of these n positions.

In that case, you can use the itertools module to do that.

Let's look at how this works:

  1. turn the Search string into a list so it can be iterated over, create empty List to hold results.
  2. 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.

  3. Each element of idxs is a tuple of n indices from 0 to len(SearchL)-1 (e.g. (0,1,2,4). Replace the i'th character of SearchL with a '$' for each i in the tuple.
  4. Convert the result back into a string and add it to List.

As an example:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文