python列表查找相同字符串_从Python列表中查找输入字符串的所有紧密匹配项
假設我們給了一個單詞,我們想找到它最接近的匹配項。不是完全匹配,而是其他單詞在模式上與給定單詞非常相似。為此,我們使用一個名為difflib的模塊,并使用其名為get_close_matches的方法。
get_close_matches
此方法是difflib模塊的一部分,為我們提供了我們指定的可能模式的匹配。下面是語法。difflib.get_close_matches(word,?possibilities,?n,?cutoff)
word:?It?is?the?word?to?which?we?need?to?find?the?match.
Possibilities:?This?is?the?patterns?which?will?be?compared?for?matching.
n:?Maximum?number?of?close?matches?to?return.?Should?be?greater?than?0.
Cutoff:?The?possibilities?that?do?not?score?this?float?value?between?0?and?1?are?ignored.
運行上面的代碼給我們以下結果-
示例
在下面的示例中,我們只說了一個單詞,還列出了需要比較的可能性或模式的列表。然后我們應用該方法以獲得所需的結果。from?difflib?import?get_close_matches
word?=?'banana'
patterns?=?['ana',?'nana',?'ban',?'ran','tan']
print('matched?words:',get_close_matches(word,?patterns))
輸出結果
運行上面的代碼給我們以下結果-matched?words:?['nana',?'ban',?'ana']
總結
以上是生活随笔為你收集整理的python列表查找相同字符串_从Python列表中查找输入字符串的所有紧密匹配项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java神雕侠侣1古墓情缘游戏攻略_《神
- 下一篇: java 以一个最高有效位为1的二进制数