如何检查其中包含N个句点的行?
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
感谢任何帮助!
答案 0 :(得分:3)
使用计数功能。
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
答案 1 :(得分:0)
编辑:或者好 - 看看@Tyler Ferraro描述的方式。
问题是你如何用你的语言定义一行?一行必须以句点结尾。所以从技术上讲,一条线只能包含0或1个周期,具体取决于你的要求。
如果它包含在''
中,如:'.'
或""
喜欢“。”,那么这是一个不同的情况,您可以使用正则表达式。
我假设您的行就像:A.BAN.DON
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等等)