Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更有效地将字符串中的多个字符映射为单个字符?_Python_Python 3.x_String_Dictionary_Replace - Fatal编程技术网

Python 如何更有效地将字符串中的多个字符映射为单个字符?

Python 如何更有效地将字符串中的多个字符映射为单个字符?,python,python-3.x,string,dictionary,replace,Python,Python 3.x,String,Dictionary,Replace,我正在寻找一种将字符组映射为单个字符的有效方法 目前,我的代码与以下代码类似: example = 'Accomodation' VOWELS = 'aeiou' CONSONANTS = 'bcdfghjklmnpqrstvwxyz' output = '' for char in example: if char in VOWELS: output += 'v' elif char in VOWELS.upper(): output +=

我正在寻找一种将字符组映射为单个字符的有效方法

目前,我的代码与以下代码类似:

example = 'Accomodation'

VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

output = ''
for char in example:
    if char in VOWELS:
        output += 'v'
    elif char in VOWELS.upper():
        output += 'V'
    elif char in CONSONANTS:
        ....
在本例中,它最终将返回
vcvvc

我想让这部分更有效率:

for char in example:
    if char in VOWELS:
        output += 'v'
    elif char in VOWELS.upper():
        output += 'V'
    elif char in CONSONANTS:
        ....
理想情况下,该解决方案将允许映射到的字符字典作为键,其值为选项列表。例如

replace_dict = {'v': VOWELS,
                'V': VOWELS.upper(),
                'c': CONSONANTS,
                ...
我不太熟悉
map
,但我希望解决方案能以某种方式利用它

研究 我在这里发现了一个类似的问题:

该问题的解决方案表明,我需要以下内容:

target = 'Accomodation'
charset = 'aeioubcdfghjklmnpqrstvwxyzAEIOUBCDFGHJKLMNPQRSTVWXYZ'
key = 'vvvvvcccccccccccccccccccccVVVVVCCCCCCCCCCCCCCCCCCCCC'
然而,我并不认为这些赋值看起来特别清晰——尽管它节省了一块
if
/
else
语句。此外,如果我想添加更多字符集,则赋值的可读性甚至更低,例如,对于不同的外来字符集


任何人,也许对内置函数有更好的了解,是否能够制作一个比上述两个例子更高效/干净的例子

我也愿意接受其他不需要使用字典的想法


解决方案应该在
python3
中这是一种使用
dict
的方法

Ex:

example = 'Accomodation'

VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

replace_dict = {'v': VOWELS,
                "V": VOWELS.upper(),
                "c": CONSONANTS
                }


print("".join(k for i in example 
              for k, v in replace_dict.items() if i in v
              )
        )
Vccvcvcvcvvc
输出:

example = 'Accomodation'

VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

replace_dict = {'v': VOWELS,
                "V": VOWELS.upper(),
                "c": CONSONANTS
                }


print("".join(k for i in example 
              for k, v in replace_dict.items() if i in v
              )
        )
Vccvcvcvcvvc

有一种更有效的方法可以创建这样一个目录:

example = 'Accomodation'

VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

replace_dict = {
    **{v: 'v' for v in VOWELS},
    **{V: 'V' for V in VOWELS.upper()},
    **{c: 'c' for c in CONSONANTS}
}

print(''.join(replace_dict[s] for s in example))

# Vccvcvcvcvvc

您的
replace_dict
想法很接近,但最好“翻转”dict“由内而外”,即将其从
{v':'aei',c':'bc'}
转换为
{a':'v','e':'v','b':'c',…}

def从目录(replace目录)获取目录(replace目录):
替换_map={}
对于cls,替换目录项()中的字符:
替换映射更新(dict.fromkeys(chars,cls))
返回替换映射
def用_图替换_(s,替换_图):
return.''join(替换映射get(c,c)表示s中的c)
元音=“aeiou”
辅音=“bcdfghjklmnpnpqrstvwxyz”
replace\u map=从目录中获取\u replace\u map\u(
{“v”:元音,“v”:元音.upper(),“c”:辅音}
)
打印(用地图替换地图(“住宿,谢谢!”,替换地图))
上面的
replace_with_map
函数保留了所有未映射的字符(但您可以使用第二个参数将其更改为
.get()
),因此输出是

vccccvccvcc,ccvccc


对你正在做的事情进行反向查找怎么样?应该是可伸缩的

VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
example = "Accomodation"
lookup_dict = {k: "v" for k in VOWELS}
lookup_dict.update({k: "c" for k in CONSONANTS})
lookup_dict.update({k: "V" for k in VOWELS.upper()})
lookup_dict.update({k: "C" for k in CONSONANTS.upper()})
''.join([lookup_dict[i] for i in example])

试试这个。不需要辅音,不仅适用于英语,也适用于俄语字母(我很惊讶):

vcvcvvvvvvvvvcvvc


我是Python新手,玩Python很有趣。让我们看看这些字典有多好。此处建议的四种算法:

  • Alex(我自己)-C运行时库风格
  • 亚当-匹配四个字符串
  • Sanyash,Rakesh,Mortz-字典(查表)
  • AKX-替换为map
  • 我在建议的代码中做了一些小的修改,以使所有的工作一致。此外,我希望将组合代码保持在100行以下,但使用四个要测试的函数达到127行,并尝试使用额外的空行数来满足PyCharm的要求。以下是第一场比赛的结果:

    地名时间总计
    1.AKX 0.6777 16.5018金牌得主!!!
    2.Sanyash 0.8874 21.5725下降31%
    3.Alex 0.9573 23.2569下降41%
    4.亚当0.9584 23.2210减速41%
    
    然后我对代码做了一些小的改进:

    VOWELS_UP = VOWELS.upper()
    
    def vowels_consonants0(example):
        output = ''
        for char in example:
            if char.isalpha():
                if char.islower():
                    output += 'v' if char in VOWELS else 'c'
                else:
                    output += 'V' if char in VOWELS_UP else 'C'
        return output
    
    这让我获得了第二名:

    地名时间总计
    1.AKX 0.6825 16.5331金牌得主!!!
    2.亚历克斯0.7026 17.1036下跌3%
    3.Sanyash 0.8557 20.8817下降25%
    4.亚当0.9631 23.3327下跌41%
    
    现在我需要剃掉这3%的胡子,获得第一名。我用列夫·托尔斯泰小说的文本进行了测试

    原始源代码:

    import time
    import itertools
    
    VOWELS = 'eaiouу'  # in order of letter frequency
    CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
    
    
    def vowels_consonants0(example):
        output = ''
        for char in example:
            if char.isalpha():
                x = 'v' if char.lower() in VOWELS else 'c'
                output += x if char.islower() else x.upper()
        return output
    
    
    def vowels_consonants1(example):
        output = ''
        for char in example:
            if char in VOWELS:
                output += 'v'
            elif char in VOWELS.upper():
                output += 'V'
            elif char in CONSONANTS:
                output += 'c'
            elif char in CONSONANTS.upper():
                output += 'C'
        return output
    
    
    def vowels_consonants2(example):
        replace_dict = {
            **{v: 'v' for v in VOWELS},
            **{V: 'V' for V in VOWELS.upper()},
            **{c: 'c' for c in CONSONANTS},
            **{c: 'c' for c in CONSONANTS.upper()}
        }
        return ''.join(replace_dict[s] if s in replace_dict else '' for s in example)
    
    
    def get_replace_map_from_dict(replace_dict):
        replace_map = {}
        for cls, chars in replace_dict.items():
            replace_map.update(dict.fromkeys(chars, cls))
        return replace_map
    
    
    def replace_with_map(s, replace_map):
        return "".join(replace_map.get(c, c) for c in s)
    
    
    replace_map = get_replace_map_from_dict(
        {"v": VOWELS, "V": VOWELS.upper(), "c": CONSONANTS, "C": CONSONANTS.upper()}
    )
    
    
    def vowels_consonants3(example):
        output = ''
        for char in example:
            if char in replace_map:
                output += char
        output = replace_with_map(output, replace_map)
        return output
    
    
    def test(function, name):
        text = open(name, encoding='utf-8')
        t0 = time.perf_counter()
        line_number = 0
        char_number = 0
        vc_number = 0  # vowels and consonants
        while True:
            line_number += 1
            line = text.readline()
            if not line:
                break
            char_number += len(line)
            vc_line = function(line)
            vc_number += len(vc_line)
        t0 = time.perf_counter() - t0
        text.close()
        return t0, line_number, char_number, vc_number
    
    
    tests = [vowels_consonants0, vowels_consonants1, vowels_consonants2, vowels_consonants3]
    names = ["Alex", "Adam", "Sanyash", "AKX"]
    best_time = float('inf')
    run_times = [best_time for _ in tests]
    sum_times = [0.0 for _ in tests]
    show_result = [True for _ in tests]
    
    print("\n!!! Start the race by permutation with no repetitions now ...\n")
    print("  * - best time in race so far")
    print("  + - personal best time\n")
    print("Note  Name Time (Permutation)")
    
    products = itertools.permutations([0, 1, 2, 3])
    
    for p in list(products):
        print(p)
        for n in p:
            clock, lines, chars, vcs = test(tests[n], 'war_peace.txt')
            sum_times[n] += clock
            note = " "
            if clock < run_times[n]:
                run_times[n] = clock
                note = "+"  # Improved personal best time
            if clock < best_time:
                best_time = clock
                note = "*"  # Improved total best time
            print("%s %8s %6.4f" % (note, names[n], clock), end="")
            if show_result[n]:
                show_result[n] = False
                print("  Lines:", lines, "Characters:", chars, "Letters:", vcs)
            else:
                print()
    print("\n!!! Finish !!! and the winner by the best run time is ...\n")
    print("Place  Name Time   Total")
    i = 0
    for n in sorted(range(len(run_times)), key=run_times.__getitem__):
        i += 1
        t = run_times[n]
        print("%d. %8s %.4f %.4f " % (i, names[n], t, sum_times[n]), end="")
        if i == 1:
            print("The winner of Gold medal!!!")
        else:
            print("Slower by %2d%%" % (round(100.0 * (t - best_time)/best_time)))
    
    导入时间
    进口itertools
    元音='eaiou#'#按字母频率顺序排列
    辅音='bcdfghjklmnpnpqrstvwxyz'
    def元音辅音S0(示例):
    输出=“”
    对于示例中的char:
    如果char.isalpha():
    x='v'如果元音中的char.lower()为'c'
    如果char.islower()或x.upper(),则输出+=x
    返回输出
    def元音和辅音1(示例):
    输出=“”
    对于示例中的char:
    如果元音中有字符:
    输出+='v'
    元音中的elif char.upper():
    输出+='V'
    辅音中的elif字符:
    输出+='c'
    辅音中的elif char.upper():
    输出+='C'
    返回输出
    def元音和辅音2(示例):
    替换[u dict={
    **{v:'v'代表元音中的v},
    **{V:'V'代表元音中的V.upper()},
    **{c:'c'表示辅音中的c},
    **{c:'c'表示辅音中的c.upper()}
    }
    返回“”。join(如果示例中的s在replace_dict else“”中为s,则为s替换_dict)
    def从目录中获取目录替换映射目录(替换目录):
    替换_map={}
    对于cls,替换目录项()中的字符:
    替换映射更新(dict.fromkeys(chars,cls))
    返回替换映射
    def用_图替换_(s,替换_图):
    return.''join(替换映射get(c,c)表示s中的c)
    replace\u map=从目录中获取\u replace\u map\u(
    {“v”:元音,“v”:元音.upper(),“c”:辅音,“c”:辅音.upper()}
    )
    def元音和辅音3(示例):
    输出=“”
    对于示例中的char:
    如果替换映射中的字符:
    输出+=字符
    输出=用图替换图(输出,替换图)
    返回输出
    def测试(功能、名称):
    text=open(名称,编码='utf-8')
    t0=时间。性能计数器()
    线号=0
    字符数=0
    vc_数=0#元音和辅音
    尽管如此:
    行号+=1
    line=text.readline()
    如果不是直线:
    打破
    字符数+=len(行)
    vc_线=功能(线)
    vc_编号+=len(vc_线)
    t0=时间。性能计数器()-t0