python - 找到*最*常见的字符串前缀 - 更好的方法?

标签 python algorithm python-2.7 prefix robust

我有一个键列表 ['foo_a','foo_b','foo_c','fnord']

此处所有类似的解决方案都假定您的文本中没有 fnord

我有这段代码可以完成这项工作:

def detect_prefix(keys):
    PCT = 0.70 # cutof 
    pre = ''
    l = len(keys)
    for i in range(0, len(max(keys, key=len))):
        keys = filter(lambda k: k.startswith(pre), keys)
        cnt = dict()
        for k in map(lambda k: k[i], keys):
            cnt.setdefault(k,0)
            cnt[k] +=1
        if cnt[max(cnt)] / float(l) >= PCT:
            pre += max(cnt)
        else:
            break
    return pre

强烈怀疑这可以做得更优雅,但我的 python-fu 现在还不够强大。

我很想听听一些建议。

编辑。 额外的背景和说明。
这些是其他开发人员放入应用程序中用于翻译的 key 。 它们应该有一个共同的前缀,但人们忘记了,他们从其他代码中剪切和粘贴。 “_”作为前缀分隔符只是一种约定。最好不要假设甚至使用分隔符。 70% 是一个完全任意的阈值。 “最普遍”或“主要”也行。
是的,这是 python 2.7,引号内的空格只是一个视觉人工制品。

最佳答案

如果您知道公共(public)前缀所需的阈值频率:

#!/usr/bin/env python
from collections import Counter
from itertools import izip_longest

strings = ['foo_a','foo_b','foo_c','fnord']
threshold = .7 * len(strings)
prefix = []
for chars in izip_longest(*strings, fillvalue=''):
    char, count = Counter(chars).most_common(1)[0]
    if count < threshold:
        break
    prefix.append(char)
print(''.join(prefix))
# -> foo_

或者您可以收集所有常见前缀及其频率,然后再决定:

#!/usr/bin/env python
from collections import Counter
from itertools import izip_longest

strings = ['foo_a', 'foo_b','foo_c','fnord']
assert len(strings) > 1
threshold = len(strings)
prefix = []
prefixes = []
for chars in izip_longest(*strings, fillvalue=''):
    char, count = Counter(chars).most_common(1)[0]
    if count == 1:
        break
    elif count < threshold:
        if prefix:
            prefixes.append((''.join(prefix), threshold))
        threshold = count
    prefix.append(char)
if prefix:
    prefixes.append((''.join(prefix), threshold))
print(prefixes)
# -> [('f', 4), ('foo_', 3)]

两个代码示例都假设存在主要前缀,即每个位置最常见的字符属于最常见的前缀。

关于python - 找到*最*常见的字符串前缀 - 更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22628428/

相关文章:

python - 使用 django_tables2 在 django 中的表中添加 CSS 样式

Python Tkinter,简单示例在 win 7 上失败

arrays - 平均数大于数组其余元素的平均数的子数组数

multithreading - 复制死线程

python - 无法在 Mac OS El Capitan 上安装 nltk

python - Tensorflow 2.2.0 错误 : [Predictions must be > 0] [Condition x >= y did not hold element-wise:] while using Bidirectional LSTM layer

python - Ubuntu、cx_Freeze 和 multiprocessing.Manager() 在 "spawn"类型进程的情况下发生冲突

algorithm - 归纳图中的顶点和 - 动态规划

algorithm - 减少错误修剪算法

python - 在使用 Python 进行流式传输之前增加音量