要删除所有一个字符的字符串吗?

时间:2018-08-15 15:24:50

标签: python regex

如果我有一个单词“饿”,并且我有一个类似['asdfd', 'hingry', 'hungre', ' hangrr']的列表

由于“ hingry”和“ hungre”是角色,我该如何获得它们?我曾考虑过使用正则表达式,但在python中使用它的经验并不丰富

2 个答案:

答案 0 :(得分:6)

您可以使用itertools.zip_longestsum

from itertools import zip_longest as _zip
d = ['asdfd', 'hingry', 'hungre', ' hangrr']
word = 'hungry' 
results = [i for i in d if sum(a == b for a, b in _zip(i, word)) >= len(word)-1]

输出:

['hingry', 'hungre']

答案 1 :(得分:5)

一旦安装SELECT col1, col2 from ( SELECT CASE col1 WHEN 'y' THEN col3 ELSE 'null' END AS col1, CASE col1 WHEN 'y' THEN 'col1Y' ELSE 'null' END AS col2 from table1 as tbl1 union all select CASE col2 WHEN 'y' THEN col3 ELSE 'null' END AS col1, CASE col2 WHEN 'y' THEN 'col2Y' ELSE 'null' END AS col2 FROM table1 as tbl2) as tbl where tbl.col1 <> 'null'; ,就很容易做到:

python-levenshtein

它会自动处理:

  1. 添加了字符
  2. 已删除字符
  3. 替换字符

最终代码可能如下所示:

>>> from Levenshtein import distance
>>> distance( 'hingry', 'hungry')
1
>>> distance( 'hungre', 'hungry')
1
>>> distance( 'hungr', 'hungry')
1