我需要在python中通過給定文件來查看程序。讓我們說acronyms.txt,然後返回多少行包含至少1個三字母首字母縮寫詞的百分比值。 例如: NSW is a very large state.
It's bigger than TAS.
but WA is the biggest!
在閱讀完之後,它應該返回66.7%,爲各行的66.7%包含三個字母的縮寫。正如你所看到的,它也被四舍五
我有代碼讀取文本文件,並在文本中輸出TLA的數量,作爲包含文本的行數的百分比。 import re
total_lines = 0
matched_lines = 0
for line in open("sentences.txt"):
total_lines += 1
matched_lines += bool(re.search(r"\b[A-Z]{3}\b", li