你如何检查其中有 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
感谢任何帮助!
你如何检查其中有 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
感谢任何帮助!
使用计数功能。
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 Ferraro 描述的方式。
问题是你如何用你的语言定义一条线?一行必须以句点结尾。因此,从技术上讲,一行只能包含 0 或 1 个句点,具体取决于您询问的对象。
如果它包含在''
like:'.'
或""
like "." 中,则情况不同,您可以使用正则表达式。
我假设你的行是这样的: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等......)