简体   繁体   English

如何用所有可能的组合替换字典中列表中的字符串

[英]How to replace strings in list from a dictionary with all possible combinations

I want to replace a string named test1 which is listed in lst using a dictionary named gRep1Map .我想使用名为gRep1Map的字典替换 lst 中列出的名为test1的字符串。 It needs to return all possible combinations using the characters in gRep1Map .它需要使用gRep1Map中的字符返回所有可能的组合。 I get an output, but not the one i want to.我得到了 output,但不是我想要的。 Can't really seem to find a way how to achieve this.似乎无法真正找到实现这一目标的方法。

Here is my code.这是我的代码。

text = "Test1"

#Create dictionary
gReplMap = { 'a': '@', 'e': '3', 'i': '1', 'o': '0', 't': '+',
             'A': '@', 'E': '3', 'I': '1', 'O': '0', 'T': '+',
}

lst = []

for old, new in gReplMap.items():
    text = text.replace(old, new)
    lst.append(text)
    print(lst)

The output is like below. output 如下所示。

['Test1']
['Test1', 'T3st1']
['Test1', 'T3st1', 'T3st1']
['Test1', 'T3st1', 'T3st1', 'T3st1']
['Test1', 'T3st1', 'T3st1', 'T3st1', 'T3s+1']
['Test1', 'T3st1', 'T3st1', 'T3st1', 'T3s+1', 'T3s+1']
['Test1', 'T3st1', 'T3st1', 'T3st1', 'T3s+1', 'T3s+1', 'T3s+1']
['Test1', 'T3st1', 'T3st1', 'T3st1', 'T3s+1', 'T3s+1', 'T3s+1', 'T3s+1']
['Test1', 'T3st1', 'T3st1', 'T3st1', 'T3s+1', 'T3s+1', 'T3s+1', 'T3s+1', 'T3s+1']
['Test1', 'T3st1', 'T3st1', 'T3st1', 'T3s+1', 'T3s+1', 'T3s+1', 'T3s+1', 'T3s+1', '+3s+1']

But i want it to be like this.但我希望它是这样的。

['test1', 'tes+1', 't3st1', 't3s+1', '+est1', '+es+1', '+3st1', '+3s+1']

Anyone who could help?有谁能帮忙吗?

You should not be iterating through the dictionary but through the letters of the text.您不应该遍历字典,而是遍历文本的字母。 Here is a solution that doesn't use itertools.这是一个不使用 itertools 的解决方案。

text = "Test1"
gReplMap = { 'a': '@', 'e': '3', 'i': '1', 'o': '0', 't': '+',
         'A': '@', 'E': '3', 'I': '1', 'O': '0', 'T': '+'}
lst = [text]
#Iterate through each letter in each word in lst and update the lst
for string in lst:
    for letter in string:
        if letter in gReplMap:
            new_string = string.replace(letter, gReplMap[letter])
            if new_string not in lst:
                lst.append(new_string)
print(lst)

(a rare case of usefulness of reduce ...) reduce有用的罕见案例......)

from itertools import combinations
from functools import reduce

out = {
    reduce(lambda x, y: x.replace(y[0], y[1]), repls, text)
    for n in range(len(gReplMap) + 1)
    for repls in combinations(gReplMap.items(), n)
}

Produces:生产:

>>> out
{'+3s+1', '+3st1', '+es+1', '+est1', 'T3s+1', 'T3st1', 'Tes+1', 'Test1'}

If you prefer to see the distinct values in the order they were generated, use a dict (which has insertion order) and make a list of it:如果您希望按生成顺序查看不同的值,请使用dict (具有插入顺序)并列出它:

out = list({
    reduce(lambda x, y: x.replace(y[0], y[1]), repls, text): 1
    for n in range(len(gReplMap) + 1)
    for repls in combinations(gReplMap.items(), n)
})

Then:然后:

>>> out
['Test1', 'T3st1', 'Tes+1', '+est1', 'T3s+1', '+3st1', '+es+1', '+3s+1']

Note: in the desired result of your original question, all the variants of 'test1' seem lowercase, but neither your sample code or the text of the question seems to specify that.注意:在您的原始问题的预期结果中, 'test1'所有变体似乎都是小写的,但您的示例代码或问题文本似乎都没有指定这一点。 Of course, use .lower() after reduce if that's what you want.当然,如果你想要的话,在 reduce 之后使用.lower()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用另一个字符的所有可能组合替换列表中字符串的第一个和最后一个元素 - How to replace the first and last elements of strings in a list with all possible combinations of chars from another 从字典和列表中获取所有可能的组合 - Get all possible combinations from dictionary and list 列出复杂的列表列表中的所有可能组合? - List all possible combinations from a complex dictionary of lists of lists? 如何从包含列表和字符串的嵌套列表中查找所有可能的组合? - How to find all possible combinations from nested list containing list and strings? 如何在字典中生成字典中所有可能的组合 - How to generate all possible combinations in a dictionary in a dictionary 如何从具有列表值的字典中获取一系列键来检索所有可能的组合 - How to retrieve all possible combinations given a sequence of keys from a dictionary with list values 集合的所有可能组合作为字符串列表 - All possible combinations of a set as a list of strings 从字典替换列表中的字符串 - Replace strings in list from dictionary Python - 如何从嵌套列表中获取所有可能的组合 - Python - How to get all possible combinations from nested list 如何从python中的列表中获取所有可能的组合以允许重复 - How to get all possible combinations from a list in python allowing repetition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM