python - 匹配字符串的字符

标签 python regex python-3.x

如果我调用 matcher.match('bad') 它应该返回提供的列表中存在的 'bad' 的所有排列的列表。在此示例中,输出将为['abd']。到目前为止,这是我尝试过的,但我似乎无法匹配这个模式。

class Matcher():
    def __init__(self, string_list):
        self.string_list = string_list

    def match(self, string_match):
        matched_list = [string for string in string_list if string_match in string]
        return matched_list

string_list = ['abd', 'abdd', 'fret', 'gilk', 'lokm']
matcher = Matcher(string_list)
print(matcher.match('abd'))

最佳答案

这是一个 O(n log n) 解决方案:

string_list = ['abd', 'abdd', 'fret', 'gilk', 'lokm']

my_str = 'bad'

[x for x in string_list if sorted(my_str) == sorted(x)]  # ['abd']

关于python - 匹配字符串的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621161/

相关文章:

python - 使用 Python 从上下文中自动选择标签

PHP正则表达式删除下划线后的所有内容

regex - Golang 正则表达式总是返回 false?

Python从命名管道获取数据

python - 语法错误,DEBUG 在 urls.py 中显示错误?

python - 比较两个大文件并合并匹配信息

python - 区域 : IOError: [Errno 22] invalid mode ('w' ) or filename

php - 如何使用正则表达式排除两个大括号之间的文本?

python - 在 python 中重写 __setattr__ 和 __getattribute__ 时,无法更新可变实例属性

python - 扬声器发出简单的蜂鸣声