python: check for lines that have X number of periods '.'
本问题已经有最佳答案,请猛点这里访问。
如何检查其中有n个句点的行?
1 2 3 4 5 6 7 8 9 10 | my_count = 6 indict = open("dictionary.txt").read().splitlines() for line in indict: # if line has my_count num of '.': print line dictionary.txt looks like: A.BAN.DON A.BOUND A.BI.DING |
谢谢你的帮助!
使用Count函数。
1 2 3 4 5 6 | my_count = 6 indict = open("dictionary.txt").read().splitlines() for line in indict: # if line has my_count num of '.': if line.count('.') == my_count: print line |
编辑:或者-看看@tyler farroro描述的方式。
问题是你如何用你的语言定义一行?行必须以句点结尾。所以从技术上讲,一行只能包含0或1个句点,这取决于你问谁。
如果它包含在
我假设你的台词是:
1 2 3 4 5 6 | for line in indict: # if line has my_count num of '.': for character in line: if character is".": dotCount += 1 print"line: %s has %d periods" % (line, dotCount) |
你可以自己安排其余的事情(比如声明dotcount等等)。