# 蟒蛇 |字符串相似度指標

| | |

` # Python3 演示代碼 `
` # 行間相似度 `
` # 使用樸素的方法 (sum() + zip()) `

` # 計算相似度的實用函數 `

` def ` ` 類似 (str1, str2): `

` str1 ` ` = ` ` str1 ` ` + ` ` `` ` ` * ` ` (` `len ` ` (str2) ` ` - ` ` len ` ` (str1) ) `

` str2 ` ` = ` ` str2 ` ` + ` ` `` ` ` * ` ` (` ` len ` ` (str1) ` ` - ` `len ` ` (str2)) `

` ` `返回` ` sum ` ` (` ` 1 ` ` if ` ` i ` ` = ` ` = ` ` j ` ` else ` ` 0 `

` for ` ` i, j ` ` in ` ` zip ` ` (str1, str2)) ` ` / ` ` float ` ` (` `len ` ` (str1)) `

` `
` # 初始化字符串 `

` test_string1 ` ` = ` `` Geeksforgeeks` `

` test_string2 ` ` = ` ` `Geeks4geeks` `

` `
` # 使用樸素方法 (sum () + zip ()) `
` # 行間相似度 `

` res ` ` = ` ` 類似 (test_string1, test_string2) `

` # 打印結果 `

` print ` ` (` ` "2個字符串的相似度為：" ` ` + ` `str ` `(res)) `

`2個字符串的相似度為：0.38461538461538464 `

` # Python3 演示代碼 `
` # 行間相似度 `
` # 使用 SequenceMatcher.ratio () `

` from ` ` difflib ` `導入` code> ` SequenceMatcher `

` # 計算相似度的實用函數`

` def ` ` 類似(str1, str2)：`

` return ` ` SequenceMatcher (` ` None ` `, str1, str2) .ratio () `

` # 行初始化`

` test_string1 ` ` = ` ` `Geeksforgeeks` `

` test_string2 ` ` = ` ` `Geeks` `

` `
` # 使用 SequenceMatcher.ratio ( ) `
` # 行間相似度 `

` res ` ` = ` ` 類似(test_string1, test_string2) `

` `
` # 打印結果 `

` print ` ` (` ` "2 個字符串的相似度為：" ` ` + ` ` str ` ` (res )) `

`2個字符串的相似度為：0.5555555555555556 `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method