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 の宣言など...)