将字典中的键与字符串匹配。而不是循环字典或字符串单词中的键

时间:2019-06-10 05:31:50

标签: python

我希望将当前键映射到字典中,并将字符映射到字符串中。

示例:

dictionary = {"test":"1", "card":"2"} # my concern is that sometimes my dictionary will have thousands of keys so I do not wish to loop each of it.

string = "There istest at the cardboards" # the string will also have a long strings.


for d in dictionary:
    if d in string:
        string = string.replace(d,dictionary[d])

如果我要用这种方式执行,那么如果我对字典和字符串的输入都很长,那将非常昂贵。

我当前的结果:

"There is1 at the 2board"

预期结果:

(相同,但不循环字典或字符串。我想学习一些更高级的方法。谢谢。)

There is1 at the 2board

5 个答案:

答案 0 :(得分:1)

使用正则表达式。

例如:

cxf-core-3.1.12.jar              
cxf-rt-databinding-jaxb-3.1.12.jar  
cxf-rt-frontend-simple-3.1.12.jar  
cxf-rt-ws-addr-3.1.12.jar    
neethi-3.0.3.jar
cxf-rt-bindings-soap-3.1.12.jar  
cxf-rt-frontend-jaxrs-3.1.12.jar    
cxf-rt-rs-client-3.1.12.jar        
cxf-rt-ws-policy-3.1.12.jar  
wsdl4j-1.6.3.jar
cxf-rt-bindings-xml-3.1.12.jar   
cxf-rt-frontend-jaxws-3.1.12.jar    
cxf-rt-transports-http-3.1.12.jar  
cxf-rt-wsdl-3.1.12.jar       
xmlschema-core-2.2.2.jar

org.apache.cxf.bus.extension.ExtensionException: Could not load extension class org.apache.cxf.ws.policy.AssertionBuilderRegistryImpl

输出:

import re

dictionary = {"test":"1", "card":"2"}
str_val = "There istest at the cardboards" 

pattern = re.compile("|".join(dictionary))                      #Create Regex pattern with keys. 
result = pattern.sub(lambda x: dictionary[x.group()], str_val)  #re.sub to replace
print(result)

答案 1 :(得分:1)

这是我的解决方案,它比for循环要快。

import time

dictionary = {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5'}

string = 'The number is one two two 9 five three 10 four'
string = string.split()

#################solution 1#####################
string1 = string.copy()
st = time.clock()

# solution one, using for loop
for i, s in enumerate(string1):
    if s in dictionary.keys():
        string1[i] = dictionary[s]

t = time.clock() - st
print('time cost: {}'.format(t))
print(' '.join(string1))

################solution 2#####################
string2 = string.copy()
st = time.clock()

# solution 2
string2 = [s if s not in dictionary.keys() else dictionary[s] for s in string2]

t = time.clock() - st
print('time cost: {}'.format(t))
print(' '.join(string2))


time cost: 5.999999999999062e-06
The number is 1 2 2 9 5 3 10 4
time cost: 2.999999999999531e-06
The number is 1 2 2 9 5 3 10 4

答案 2 :(得分:0)

在这里尝试此解决方案:

dictionary = {"test":"1", "card":"2"}
keys = list(dictionary)
print(keys)
['test', 'card']
string = "There istest at the cardboards"
temp_string = string
for i in keys:
    if i in temp_string:
        temp_string = temp_string.replace(i,str(keys.index(i)+1))

print(temp_string)
'There is1 at the 2boards'

答案 3 :(得分:0)

通过使用reduce

dictionary = {"test":"1", "card":"2"}
string = "There istest at the cardboards"

from functools import reduce
res = reduce(lambda k, v: k.replace(v, dictionary[v]), dictionary, string)

## 'There is1 at the 2boards'

答案 4 :(得分:0)

将字典中的所有键组合为一个巨大的正则表达式: (test|card|...)

请确保一次编译该正则表达式(regex)。那是最昂贵的手术。

随后,使用编译的正则表达式在输入字符串中搜索匹配项。大括号()定义了一个组。这意味着当您找到一个匹配项时,您可以检索该组的内容,因此您将知道是否找到了“测试”,“卡片”或其他候选者之一。然后使用该值查找替换项。

当您遍历字符串时,我不建议更改该字符串。相反,构建第二个字符串,在其中复制原始字符串的未更改部分,并根据需要从字典查找中复制替换内容。


不循环字典或字符串

如果不循环两者,就无法做您想做的事情。您可能会找到一些声明性的方法来描述所需的内容,但是在最下面的地方,处理器必须通过 execute 遍历字典和字符串来计算所需的内容。 您可能希望实现的最好结果是,处理器仅循环一次(n + m次迭代),而不是运行嵌套循环(n * m次迭代)。