Let's go through a possible answer together.
Here I've written out a candidate example.
Let's image that "bell hooks" is p and "curer bell" is q.
The first thing we're going to do is find the index into p of the first space.
Pindex space 0, 1, 2, 3, 4. This'll be 4 in our running example.
Qindex--the location of this space in q might be different--0, 1, 2, 3, 4, 5.
In fact in our example it is.
Now what I want to do is select out the first word in p.
I want everything from the beginning all the way up to, but not including, the space.
This selection goes to the left as far as it can but does not include pindex,
so it will be "bell."
I could also have put a 0 in here. That would have been exactly the same.
For q I don't want to start at position 5.
I actually want to start at position 6, starting with the b and going to the end of the word.
This is q's second word, which in our running example is also bell,
since bell equals bell, we'll just return True.
Curer Bell was actually a pseudonym, a false name, adopted by Charlotte Bronte, an author.
Vediamo insieme la possibile risposta.
Ho scritto qui degli esempi plausibili.
Immaginiamo che 'bell hooks' è p e 'curer bell' è q.
La prima cosa che faremo è trovare l'indice in p del primo spazio bianco.
p.index() space 0, 1, 2, 3, 4. Sarà quindi 4 nel nostro esempio.
q.index() -- la position di questo spazio in q sarà differente -- 0, 1, 2, 3, 4, 5.
Ed infatti così è.
Adesso quello che voglio fare prendere la prima parola in p.
Voglio prendere tutto quello che c'è dall'inizio della parola, senza lo spazio.
Questa selezione procede da sinistra sino alla fine, ma non include p.index(),
quindi avremo 'bell'.
Potre aver posto uno 0 qui. Sarebbe stato lo stesso.
Per q non voglio cominciare dalla posizione 5.
Voglio cominciare dalla posizione 6, cominciando con "b" sino alla fine della parola.
Questa è la seconda parola di q, che nel nostro esempio è essa stessa 'bell'.
Dato che bell == bell, faremo in modo di ritornare True come risultato.
Curer Bell era lo pseudonimo adottato da Charlotte Bronte, l'autrice.
単語をスペースで分けることは
コンピュータ・サイエンスではよく行われます
Pythonにはこれを実行する
string.split関数が組み込まれています
例えばシャーロット・ブロンテの小説
“Jane Eyre”を文字列として使い
split関数を呼んでみます
まずリストが戻り値として返されて
このリストの要素はそれぞれ
スペースで区切られた文字列にある単語に
符合します
このスペースはこのカンマに置き換えられ
リストの要素を分けています
では少し時間をとって
ホワイトスペースによる分割という
新しいアプローチを練習してみましょう
ホワイトスペースとは
スペースやタブなどのフォーマルな言い方です
ここで3つのPython表現を書きました
どれも定数文字列です
split関数を呼び出します
split関数によって返ったリストにある要素の数を
各ボックスに入れてください
インタプリタを使ってもいいですが
まずは自力で解いてみてください
Vejamos aqui uma possível solução.
Aqui está nosso exemplo.
Suponha que "bell hooks" é p e "curer bell" é q.
Primeiro vamos encontrar o indice do primeiro espaço em p.
pindex 0,1,2,3,4. O indice é 4 no nosso exemplo.
qindex - a posição do espaço em q pode ser diferente - 0,1,2,3,4,5.
No nosso exemplo, é diferente.
Agora queremos selecionar a primeira palavra em p.
Queremos tudos os caracteres, do início até o espaço, excluindo o espaço.
Essa seleção se estende à esquerda até pindex, não incluindo pindex.
Então, a primeira palavra é "bell".
Eu poderia também colocar 0 aqui. Seria exatamente a mesma coisa.
No caso de q, não queremos começar na posição 5.
De fato, queremos começar na posição 6, começando com "b", até o final da palavra.
Essa é a segunda palavra de q, que no nosso exemplo é também "bell".
como "bell" é igual a "bell", retornamos True.
Curer Bell é um pseudonimo, um nome fictício, adotado por Charlote Bronte, um escritor.
让我们一起来找出一个可能的答案
这里我已经写出一个候选的例子
假设p是“bell hooks”,q是“curer bell”
我们要做的第一件事是在p里找到第一个空格的索引
pindex, 0, 1, 2, 3, 4,在我们的例子里就是4
qindex -- 在q里空格的位置可能不一样 -- 0, 1, 2, 3, 4, 5
实际上在我们的例子里就是这样
既然我想挑选出p里的第一个单词
那么我想要从开始一直到空格的位置,但是不包含空格
这个选择尽可能获取左边的内容,但是不包含pindex所在的位置
所以,结果就是“bell”
我也可以在这里写上0,结果是完全一样的
对于q,我不想从位置5开始
实际上我想从位置6开始,从b开始一直到单词的末尾
这就是q的第二个单词,在我们的例子里也是“bell”
因为bell等于bell,我们就返回Ture
实际上Curer Bell是作家Curer Bell的笔名,是一个假名字