[Narrator] I hope you're getting a sense that lists are very powerful.
That by using mutation, by having lists that could contain other lists
or any other kind of data we want,
we can build very complex data structures, and we can do lots of interesting things.
I'm going to introduce 1 more list operation,
which will allow us to add a new element at the end of the list.
We've seen that we can use lists to store complex data,
that elements of the list can be any type we want including other lists,
and we've seen that we can use mutation to change the value of a list,
and that mutation is visible through any reference to the same list object.
Now I'm going to introduce some other list operations.
The first one is append, and append is like a procedure but it's a method,
so we use it similar to the way we use to find on strings.
We'll have a list first, then a dot followed by append,
and what we pass in is the element we want to append to the list.
Append will add a new element to the end of a list,
and the important thing about append is that it's mutating
the list that its invoked on.
It's not creating a new list; it's mutating the old list.
As an example of the use of append let's assume that instead of
replacing curly in the 3 stooges, we want to end up with 4 stooges.
We'll add Shemp and add the other 3 as they are.
So what we want to do is to append Shemp at the end of the list we have.
We would do that by invoking append on the stooges,
passing in the string Shemp as the input.
Here's what happens after the first assignment,
the name stooges refers to the list containing the 3 elements,
Moe, Larry, and Curly.
When we invoke append it modifies that object,
adding a new element to it.
After the append, the list that stooges refers to now has 4 elements.
We have not created a new list.
Note that there's no assignment from the result of append.
What we've done is modify the value that stooges refers to
by adding a new element to it.
Spero stiate cominciando a capire che le liste sono uno strumento molto potente.
Usando la mutazione, e potendo avere liste che contengono altre liste
o qualsiasi altro dato,
possiamo costruire strutture di dati molto complesse, permettendoci di fare cose molto interessanti.
Vediamo ora un'altra operazione possibile sulle liste,
che ci permetterà di aggiungere nuovi elementi alla fine della lista.
Abbiamo visto che possiamo usare le liste per immagazzinare dati complessi,
questi elementi della lista possono essere di qualsiasi tipo volessimo, incluse altre liste,
e possiamo usare la mutazione per cambiarne il valore,
questo è visibile per ogni riferimento che facciamo allo stesso oggetto-lista.
Vediamo ora altre operazioni sulle liste.
La prima è 'append()' . 'append()' funziona come una procedura ma è un metodo,
la utilizziamo quindi come abbiamo usato 'find()' sulle stringhe.
Avremo una lista, poi un punto seguito da 'append()' ,
e quello che passiamo all'interno è l'elemento che vogliamo aggiungere alla lista.
'append()' aggiungerà un nuovo elemento in coda alla lista,
la cosa importante è che stiamo mutando
la lista su cui invochiamo 'append()' .
Non creiamo una nuova lista; ma mutiamo la vecchia.
Come esempio sull'uso di 'append()' assumiamo di
rimpiazzare 'curly' nei tre 'stooges', vogliamo avere 4 'stooges' .
Aggiungiamo 'Shemp' agli altri tre.
Quello che vogliamo è aggiungere 'Shemp' alla fine della lista di partenza.
Invocheremo 'append()' su 'stooges' ,
passandogli la stringa 'Shemp' come input.
Questo è quello che succede dopo la prima assegnazione,
il nome 'stooges' si riferisce a questa lista di tre elementi ,
Moe, Larr y e Curly.
Se invochiamo 'aapend()' andiamo a modificare questo oggetto,
aggiungendo un nuovo elemento.
Dopo 'append()' la lista ha ora quattro elementi.
Non abbiamo creato una nuova lista.
Nota che non c'è assegnazione per il risultato di 'append()' .
Abbiamo solo modificato il valore a cui 'stooges' si riferisce
appendendo un nuovo elemento alla lsita.
リストがとても役立つことが分かりましたか
ミューテーションを使用し別のリストやその他の
任意のデータを含むリストを持つことで
複雑なデータ構造を行えます
リスト操作をもう1つ紹介します
これはリストの終わりに新しい要素を
加えることができるものです
複雑なデータを保存するために
リストを使用することを学びました
別のリストを含む任意のあらゆるものが
リストの要素になります
リストの値を変更するための
ミューテーションを学びました
ミューテーションは同じリストオブジェクトの
参照を通して可視になります
では別のリスト操作をいくつか紹介します
1つ目はappendです 関数ではなくメソッドです
なので文字列を見つける時に使用する方法を
同じように使用します
リストとドット それに続くappendがあります
渡すものはリストに加えたい要素です
appendはリストの末尾に新たな要素を追加します
appendで重要なことは呼び出されているリストを
変更するということです
新しいリストを作成するのではありません
元のリストを変更するのです
appendの使用例として三バカ大将の
カーリーを置き換える代わりに
三バカにシェンプを加えた四バカ大将にしましょう
リストの末尾にShempを加える必要があります
まずstooges.appendを呼び出します
文字列Shempを引数として渡します
これが1つ目の代入を行った時に起きることです
stoogesの名前は3つの要素
Moe、Larry、Curlyを含むリストを参照します
appendを呼び出すと新たな要素が追加され
そのオブジェクトが変更されます
appendのあとstoogesが参照するリストが
4つの要素になります
リストを新たに作成せず
appendの結果を代入しないことに注意してください
行ったのは新たな要素を加えることで
stoogesが参照する値を変更したことです
Eu espero que você esteja percebendo que listas são muito poderosas.
E que, usando mutação, tendo listas que contêm outras listas,
ou qualquer outro tipo de dado que desejarmos,
podemos contruir estruturas de dados bem complexas, e podemos fazer várias coisas interessantes.
Eu vou introduzir mais uma operação sobre listas,
que nos permitirá adicionar um novo elemento no final de uma lista.
Já vimos que podemos usar listas para armazenar dados complexos,
que elementos de uma lista podem ser de qualquer tipo que quisermos, inclusive outras listas,
e vimos que podemos usar mutação para modificar o valor de uma lista,
e que mutação é visível por meio de qualquer referencia para o mesmo objeto lista.
Agora, eu vou introduzir mais algumas operações sobre listas.
A primeira é append: append é similar a um procedimento, mas é um método,
e por isso nós usamos append de modo semelhante ao que usamos find sobre strings --
nós temos primeiro uma lista, seguida de um ponto, seguido de append,
e o que passamos como argumento é o elemento que queremos adicionar à lista.
append irá adicionar um novo elemento ao final da lista.
E o importante sobre append é que ele muda
a lista sobre a qual ele é invocado.
Ele não cria uma nova lista; ele muda a lista dada.
Como um exemplo do uso de append, vamos supor que, ao invés de
subrtituir "Curly" em stooges, queremos ter agora 4 patetas:
vamos adicionar "Shemp" e manter os três outros como estão.
Então, o que queremos fazer é adicionar "Shemp" no final da lista que já temos.
Isso pode ser feito invocando append sobre stooges,
passando como entrada a string "Shemp".
Aqui está o que acontece depois da primeira atribuição:
o nome stooges referencia uma lista contendo 3 elementos,
"Moe", "Larry" e "Curly"
e, quando invocamos append, ele modifica este objeto,
adicionando um novo elemento a ele.
Depois do append, a lista referenciada por stooges tem agora 4 elementos.
Nós não criamos uma nova lista.
Note que não existe uma atribuição do resultado de append --
o que fizemos foi modificar o valor referenciado por stooges,
adicionando um novo elemento a ele.
希望大家已经了解 链表功能强大
使用可变 嵌套链表
以及其他类型的数据
我们可以建立复杂的数据结构 写有趣的代码
接下来我还将介绍一种链表操作
让我们能够在链表结尾添加新的元素
大家已经知道 链表可以用来存储复杂的数据
链表的元素可以是任何类型的数据 包括嵌套链表
我们也学习了如果运用可变来改变链表的值
由于链表可变 所以如两个变量指向同一链表 那么改变一个链表的值 另一个链表的值也会改变
现在我将介绍另外几种链表操作
首先是Append(添加) 这是一种方法
使用起来和字符串中查找方法类似
链表在前 然后有一点 之后写append(添加)
再传入我们想添加到链表的元素
append方法会在链表末尾新增元素
要记住 append方法改变了
该操作作用的链表
并未创立新链表 而是改变了原有链表的值
举个例子 假定
三个stooges中 我们不取代Curly 而是加入第四个stooge
保留原有三个元素 在最末尾加入Shemp
所以现在需要添加Shemp
要对变量Stooges使用Append方法
传入字符串Shemp
第一句赋值语句后
变量Stooges代表包含3个元素的链表
Moe Larry 和Curly
使用Append方法后 改变了对象
新增了一个元素
使用Append方法后 链表Stooges现有四个元素
我们并没有建立新链表
注意 使用Append方法之后并没有赋值
而是改变了Stooges的值
新增了一个元素
希望你們了解,列表非常具有威力
可以使用 mutation、可以在列表中包含其他的列表
以及其他類型的資料
我們可以建立非常複雜的資料結構 (data structure),
可以做很多有趣的事
我還要介紹一種列表操作
它允許我們在列表結尾加入新的元素
我們已經看到,可以使用列表儲存複雜的資料
列表的元素可以是任何類型的資料,包括其他的列表
我們也學習到運用 mutation 來改變列表的值
透過指向同一個列表物件,可以看到 mutation
現在我要介紹另外幾種列表操作
首先是 append,append 很像程序,
但是它是一種方法 (method)
使用起來和字串的 find 類似
先是一個列表,然後是一點,後面接著 append
然後,傳入我們想添加到列表的元素
append 會在列表末尾增加新的元素
重點是 append
改變 (mutating) 了呼叫它的列表
它沒有建立新列表,而是改變 (mutating) 舊的列表
舉個使用 append 的例子
假設我們不在 3 個 stooges 中換掉 Curly,
而是變為 4 個 stooges
保留原有三個元素,在最末尾加入 Shemp
所以現在要在列表末尾加入 Shemp
要對 stooges 呼叫 append
傳入字串 Shemp 作為輸入
在第一個指定陳述後
stooges 指向包含 3 個元素的列表
'Moe','Larry',和 'Curly'
當我們呼叫 append ,它改變了物件
加入一個新的元素
使用 append 後, stooges 指向的列表,現在有了 4 個元素
我們沒有建立新列表
請注意,對於 append 的結果沒有做指定陳述
我們做的是,修改了 stooges 指向的值
加入一個新的元素