0

そこで、anhCrawler を数えて「DEATH STAR」の位置だけでスペースありとなしの文字数を返し、レポートで返すようにしています。数字も正しくカウントできません。助けてください!

anhCrawler = """Episode IV, A NEW HOPE. It is a period of civil war. \
Rebel spaceships, striking from a hidden base, have won their first \
victory against the evil Galactic Empire. During the battle, Rebel \
spies managed to steal secret plans to the Empire's ultimate weapon, \
the DEATH STAR, an armored space station with enough power to destroy \
an entire planet. Pursued by the Empire's sinister agents, Princess Leia\
races home aboard her starship, custodian of the stolen plans that can \
save her people and restore freedom to the galaxy."""

theReport = """
This text contains {0} characters ({1} if you ignore spaces).
There are approximately {2} words in the text. The phrase
DEATH STAR occurs and starts at position {3}.
"""

def analyzeCrawler(thetext):
numchars = 0
nospacechars = 0
numspacechars = 0
anhCrawler = thetext
word = anhCrawler.split()
for char in word:
    numchars = word[numchars]
    if numchars == " ":
        numspacechars += 1
anhCrawler = re.split(" ", anhCrawler)
for char in anhCrawler:
    nospacechars += 1
numwords = len(anhCrawler)
pos = thetext.find("DEATH STAR")
char_len = len("DEATH STAR")
ds = thetext[261:271]
dspos = "[261:271]"

return theReport.format(numchars, nospacechars, numwords, dspos)
print analyzeCrawler(theReport)
4

3 に答える 3

2

あなたはこの問題を考えすぎています。

文字列の文字数 (520 を返します):

len(anhCrawler)

文字列内の空白以外の文字の数 (を使用splitするとsplit自動的に空白が削除され、空白joinのない文字列が作成されます) (434 を返します):

len(''.join(anhCrawler.split()))

「DEATH STAR」の位置を見つける (261 を返す):

anhCrawler.find("DEATH STAR")
于 2015-02-09T03:03:32.817 に答える
1

まず、関数内のコードをインデントする必要があります。2番目...コードは次のように簡略化できます。

theReport = """
    This text contains {0} characters ({1} if you ignore spaces).
    There are approximately {2} words in the text. The phrase
    DEATH STAR is the {3}th word and starts at the {4}th character.
"""

def analyzeCrawler(thetext):

    numchars = len(anhCrawler)
    nospacechars = len(anhCrawler.replace(' ', ''))
    numwords = len(anhCrawler.split())

    word = 'DEATH STAR'
    wordPosition = anhCrawler.split().index(word)
    charPosition = anhCrawler.find(word)

    return theReport.format(
        numchars, nospacechars, numwords, wordPosition, charPosition
    )

formatの意味が明確ではなかったので、最後の 2 つの引数を変更しましdsposた。いずれにせよ、代わりに単語と文字の位置を含めました。本当に含めるつもりだったものを判断できます。

于 2015-02-09T03:08:49.137 に答える
1

ここに、関数の単純化されたバージョンがあります。

import re

def analyzeCrawler2(thetext, text_to_search = "DEATH STAR"):

    numchars = len(anhCrawler)
    nospacechars = len(re.sub(r"\s+", "", anhCrawler))
    numwords   = len(anhCrawler.split())
    dspos      =  anhCrawler.find(text_to_search)

    return theReport.format(numchars, nospacechars, numwords, dspos)



print analyzeCrawler2(theReport)


This text contains 520 characters (434 if you ignore spaces).
There are approximately 87 words in the text. The phrase
DEATH STAR occurs and starts at position 261.

トリックの部分は、文字列から空白を削除し、空白以外の文字数を計算することだと思います。これは、正規表現を使用して簡単に実行できます。残りは自明であるべきです。

于 2015-02-09T03:08:53.247 に答える