So there are lots more things we can do with
strings. The next one I am going to talk about, is
selecting sub-sequences from strings. So what we have seen so
far, we have used indexing, where we have a string,
where we have our square bracket. We have some expression
that produces one number, and we have our closed square
bracket, and that gives us a one-character string. And whatever
position the number refers to in the string here. The other
thing we can do with square brackets is select
a sub-sequence of the string. Instead of just having
one expression here, what we can have is an
expression. This is also something that should evaluate to a
number, followed by a colon, followed by another expression.
Both of these expressions are numbers. And this will
evaluate to a string, this is a subsequence of
a characters in, the input string. So I called the
string s, the value of this number will call
start, and the value of this number will call
stop. And what the result is, is a string
that's a subsequence of all the characters in s,
and the string that we had here. Starting from
position start, so the number to the left of
the colon, and ending with position stop, but not
including that character. So it's going to really include the characters
from position start, up through stop minus 1. So this construct allows us
to select from any string A subsequence of continuous characters in that string.
So, let's try an example in the Python interpreter.
I'm going to initialize the variable word to be the
string assume. And using the single index operator we
saw initially, we can select a character from word.
So, if I select word index 3. That will
give us the fourth character, zero, one, two, three,
which is u, and when we run that, we
see the result is u. With the new operation,
that's the same as selecting word 3:4,
that's going to select starting from position three, going
to just before position four. So that will end up being just the one letter u.
Just to make this clear, if I selected
3:3, there's no string there, that's just a
single empty string which prints out in a
way we can't see it. There's no characters
between 3 and character 3. We'll go back to
just selecting index 3. I could select from position 4
through position 6. That would give us a string which
is a subsequence of letters at position 4 and position
5. So when we run this now we see the
first one prints out to u. The second one prints
out me which is position 4 and position 5. The
last two characters in the word. I'll show you one
other thing we can do. Which is, leave one of
the sides of the colon empty. So it we did 4:
to the end, that will select from position 4 to the
end. We don't need to actually count the characters to know
where the end is. So that will produce the same thing
as we did before. We can also leave nothing in front
of the colon. That will select from the beginning. So if
we do :2, that will select from position 0 up to
position 2, giving us the first two letters of the word, as. And we could
leave both sides blank, selecting word, [:],
with nothing on either side, well, it starts from the beginning, goes to the
end. That's going to give us the whole word. There's
no good reason to ever want to do this, but
it's consistent with the syntax where we can leave
one side of the colon empty and that means
selecting from either the beginning or the end.
文字列で多くのことができます
次に紹介するのは
文字列から部分列を選択することです
これまでにインデックスを使う方法はやりました
文字列があります
角括弧のあとに1つの数値を作り出す式
そして角括弧を閉じます
1文字の文字列が与えられます
どの位置でもこの文字列が参照されます
角括弧で他にできるのは
文字列の部分列を選ぶことです
1つの数式をここに置く代わりに
数値を導き出す式 それに続いて
コロン さらに続けて別の式を置くことができます
この2つの式は数値になり
そして文字列を評価します
これは入力した文字の部分列で
文字列sと呼びます
この数値がstartを呼び出します
この数値がstopを呼び出します
その結果は文字列です
ここにある文字列sの全部の文字の部分列になります
start位置つまりコロンの左側の数値から始まり
stop位置で終わります 文字は含みません
ですから実際に
start位置からstop-1まで文字を組み込みます
この構造体によって
その文字列の連続した文字の部分列を
文字列から選択することができます
Pythonインタプリタで試しましょう
変数wordを文字列assumeに設定します
最初に確認した1つのindex演算子を使用し
wordから文字を選択します
もしword[3]を選んだら
4番目の文字が与えられます 0、1、2、3
つまりuです これを実行すると
uであると確認できます 新たな操作を行います
これはword[3:4]を選ぶのと同じです
3の位置から4の位置の直前までの文字列を選びます
なので結局1つの文字uになります
もっと詳しく見ましょう [3:3]を選ぶとします
そこに文字列はありません
1つの空の文字列で表示しても何もありません
3と文字3の間に文字はありません
[3]の選択に戻ります
4の位置から6の位置まで選ぶとします
すると4の位置と5の位置の文字の
部分列である文字列が与えられます
これを実行します
1つ目はuを表示します
2つ目は4の位置と5の位置のmeが表示されます
単語の最後の2文字です
もう1つできることを紹介します
それはコロンの片側を空白にすることです
4の位置から終わりまでを選択できます
終わりがどこか文字を数える必要はありません
これで以前と同じものが作られます
コロンの前も空白にすると
最初から選択できます
[:2]とすると0の位置から2の位置まで選択できます
wordの最初の2文字asが表示されます
両側を空白にすることもできます
word[:]を選択します
これは最初から最後までを意味します
単語の全部が表示されます
わざわざこれを行う意味はありませんが
コロンの片側を空白にすると
始めからか終わりまでを
選択できるという構文と一致します
Karakter dizileri ile
yapabileceğimiz daha bir çok şey var.
Şimdi size bir
karakter dizisinden alt dizi ayıklamayı
göstereceğim. Şu ana kadar dizide
köşeli parantez kullanarak index
kullanmayı gördük. Köşeli parantez
içindeki rakam dizinin belli bir
noktasındaki tek karakteri
bize veriyordu. Yani dizideki bulunduğu
pozisyonu
bu şekilde kullanıyorduk. Köşeli parantez
kullanarak bir diziden alt diziyi seçip
alabiliriz.
Yani sadece bir karakterlik bir dizi
yerine bir ifade oluşturabiliriz. Burada
yine sayı
kullanacağız, ancak bu kez sayı, iki nokta
üstüste ve sonra tekrar
sayı şeklinde kullanacağız.
Her iki ifadede sayı olacak. Bu bize dizi
içindeki alt diziyi verecek. İlk rakam
alt diziye dahil, son rakam hariç olacak.
Mesela af
我们可以对字符串进行许多相关操作
接下来我要讲的是从字符串中挑选子字符串
到目前为止我们接触过
使用过索引 在这个过程中会用到字符串 方括号
能产生一个数字的语句
以及闭方括号
这样我们得到一个包含一个字符的字符串
无论这个数字在字符串中指向什么
我们可以通过方括号进行的另外一个操作是从字符串中挑选子字符串
与方括号中只放置一个语句不同
在这里 我们可以输入一个语句
这个语句计算出来也应当是一个数字
然后是冒号 再之后是另一个语句
这两个语句得出的值都是数字
这样求值之后会得出一个字符串
这个字符串是输入字符串的子字符串
我将这个字符串称之为 s 将这个数字的值称为 start
这个数字的值称为 stop
得出的结果是 s 字符串中
所有字符的一个子字符串
就是刚刚我们在这提到的字符串 从 start 位置开始
冒号左边的数字
在 stop 位置结束 但不包括最后一个字符
因此实际包括的字符是从 start 位置开始 到倒数第二个字符
这种结构使得我们可以从任何字符串中
选择一个连续字符的子字符串
我们在 Python 解释器中举个例子
将变量 word 进行初始化设置 使它成为 assume 字符串
通过使用我们最初看到的单一索引运算符
我们可以从 word 中选择一个字符
如果对 word 第3个位置进行索引
我们会得到第4个位置相应的字符 0 1 2 3
第4个位置的字符是 u 当我们运行之后
我们得到的结果是 u 如果练习新讲的操作
那就是我们搜索第3个位置到第4个位置的字符
选择范围是从第3个位置开始 到第4个位置之前
因此字母 u 之前的字符为最后一个字符
为了解释得更清楚一些
如果我选择的范围是 3:3 结果显示是没有字符
输出的结果是一个空字符串 只是我们看不到
在第3个位置和字符3之间没有字符
我们回到
对字符串的第3个位置进行索引的操作
选择第4个位置到第6个位置的字符
我们将得到一个字符串,它是第4个位置和第5个位置的子字符串5
运行后可以看到
第一个输出的结果是 u
第2个输出的是第4个位置和第5个位置的字符 me
是单词的最后两个字符
下面展示另一个操作 就是让冒号的一侧没有任何内容
例如我们选择显示的范围是4:
得到的结果将会显示从第4个位置到末尾的字符
我们不需要明确地数字符来确定末尾字符所在的位置
这个输出的结果和我们刚刚操作输出的结果一样
我们也可以设置冒号前面什么内容也没有
这就意味着从字符串开始位置进行选择
因此如果我们选择的范围是:2 输出的结果将显示从第0个位置到第2个位置的字符
也就是单词的前两个字母 as
我们也可以将冒号两侧都设置为空白 选择 word [:] 范围之内的字符
冒号两边什么内容也没有 说明范围从起始位置到末尾位置
输出的结果会是 word 的完整内容
其实没有什么好的理由能解释为什么这么做
但是这与句法规则是一致的
我们可以设置冒号的一侧为空白
这说明我们从起始位置开始选择或者选择的范围到末尾位置