提问人:Samuele1818 提问时间:11/15/2023 更新时间:11/15/2023 访问量:90
Python:在没有外部库的情况下计算连续字符的最快方法
Python: Fastest way to count contiguously chars without external library
问:
我有一个文本字符串,例如:
33 P DDD AA 00 23
我想计算所有连续的字符,以创建一个带有这些字符和计数器翻译的新字符串。
我创建了一个运行良好的代码,但我需要缩短执行时间,因此我正在寻找其他方法来做到这一点。
translate_dict = {
"H": "Ab",
"I": "Bb",
"J": "Cb",
"K": "Db",
"L": "Eb",
"M": "Fb",
"N": "Gb",
"O": "A#",
"P": "B#",
"Q": "C#",
"R": "D#",
"S": "E#",
"T": "F#",
"U": "G#",
"0": "A",
"1": "B",
"2": "C",
"3": "D",
"4": "E",
"5": "F",
"6": "G",
" ": "P",
}
res = []
i = 0
duration = 0
while i < len(t):
counter = 1
while i+counter < len(t) and t[i] == t[i+counter]:
counter +=1
res.append(translate_dict.get(t[i], t[i]) + str(counter))
duration += counter
i += counter
return ''.join(res), duration
如果您需要更多信息,请随时询问!谢谢。
答:
0赞
Sash Sinha
11/15/2023
#1
考虑使用 itertools。groupby
,它内置于 Python 中,即不是外部库:
from itertools import groupby
def old_translation(t: str, translate_dict: dict[str, str]) -> tuple[str, int]:
res = []
i = 0
duration = 0
while i < len(t):
counter = 1
while i + counter < len(t) and t[i] == t[i + counter]:
counter += 1
res.append(translate_dict.get(t[i], t[i]) + str(counter))
duration += counter
i += counter
return ''.join(res), duration
def new_translation(t: str, translate_dict: dict[str, str]) -> tuple[str, int]:
translated = ''.join(
translate_dict.get(k, k) + str(len(list(g))) for k, g in groupby(t))
duration = len(t)
return translated, duration
translate_dict = {
"H": "Ab",
"I": "Bb",
"J": "Cb",
"K": "Db",
"L": "Eb",
"M": "Fb",
"N": "Gb",
"O": "A#",
"P": "B#",
"Q": "C#",
"R": "D#",
"S": "E#",
"T": "F#",
"U": "G#",
"0": "A",
"1": "B",
"2": "C",
"3": "D",
"4": "E",
"5": "F",
"6": "G",
" ": "P",
}
test_string = "33 P DDD AA 00 23"
print(f'{old_translation(test_string, translate_dict) = }')
print(f'{new_translation(test_string, translate_dict) = }')
输出:
old_translation(test_string, translate_dict) = ('D2P1B#1P1D3P1A2P1A2P1C1D1', 17)
new_translation(test_string, translate_dict) = ('D2P1B#1P1D3P1A2P1A2P1C1D1', 17)
1赞
gog
11/15/2023
#2
下面是一个可能的优化:
def f2(t):
res = []
last = t[0]
counter = 0
duration = 0
tr = translate_dict.get
for c in t:
if c == last:
counter += 1
continue
res.append(tr(last, last) + str(counter))
duration += counter
last = c
counter = 1
res.append(tr(last, last) + str(counter))
duration += counter
return ''.join(res), duration
在我的机器上显示 ~ 2 倍的改进。
评论