0

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

どんな助けにも感謝します!

4

2 に答える 2

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
于 2013-03-05T03:30:12.353 に答える
0

編集: またはまあ — @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 の宣言など...)

于 2013-03-05T03:34:01.367 に答える