Placeholder Image

字幕列表 影片播放

  • Welcome back for Unit 3.

    歡迎回到第三單元

  • This unit introduces the next big idea we need for a web crawler,

    這單元介紹下一個建立網頁蜘蛛所需的重要概念

  • which is structured data.

    也就是 “結構化資料” (structured data)

  • And by the end of this unit you will have finished building a working web crawler.

    在這單元結束時,你將完成一個網頁爬蟲 (web crawler)

  • The closest thing we've seen so far to structured data

    我們目前見過,最接近 “結構化資料” 的例子就是

  • is the string data type introduced in Unit 1 and used

    第一單元所介紹的字串資料類型 (data type)

  • in many of the procedures in Unit 2.

    它也使用在第二單元的許多程序裡

  • A string is a kind of structured data, and that's because

    字串是一種結構化資料

  • we can break it down into its characters.

    是因為我們能將它分解為字元

  • The string has a sequence of characters,

    字串有一連串的字元

  • and we can operate on sub sequences of the string.

    我們可以對字串的次序列 (sub sequences) 進行操作

  • What we could do with strings was somewhat limited, though,

    然而,我們能對字串做的事頗為有限

  • because the only thing we can put in a string is a character.

    因為我們在字串中只能放字元

  • Today, we're going to introduce the list data type,

    今天我們將介紹列表 (list) 資料類型

  • and lists are much more powerful than strings,

    列表 (lists) 比字串的功能更加強大

  • so whereas for a string, all of the elements had to be characters,

    字串中所有的元素都必須是字元

  • in a list, the elements can be anything we want.

    而列表 (list) 的元素則沒有限制

  • They could be characters. They could be strings.

    可以是字元,也可以是字串

  • They could be numbers. They could also be other lists.

    可以是數字,也可以是其他列表 (list)

  • Let's look at an example.

    我們來看個例子

  • When we created a string, we just put a sequence of characters

    創立字串時,我們只需放入一連串的字元

  • surrounded by either single or double quotes.

    用單引號或雙引號括起來

  • Here's an example of a string,

    這是字串的例子

  • and we could store that string in a variable by using an assignment.

    我們可以用指定 (assignment) 將字串儲存在變數裡

  • With a list, instead of using quotes to identify the list

    對於列表,我們不以引號來辨識列表 (list)

  • we use square brackets, and the elements are separated by commas.

    我們使用中括號,並且以逗點來分隔列表中的元素

  • And just like with a string, we can assign the list that we created

    和字串一樣,我們也能指定列表給變數

  • to a variable, so we'll store that list in the variable "p."

    現在我們將列表儲存於變數 p

  • With a string, we could use the square brackets

    對於字串,我們可以用中括號來選擇元素

  • to select elements, and when we index element 0,

    當我們指標到元素 0

  • we'll get the first element of the string, a sequence of that character,

    會得到字串的第一個元素,該字元的序列

  • which is the character "y."

    字元 'y'

  • With lists, we can also use square brackets to access elements,

    對於列表,我們也能使用中括號來取得元素

  • so if we do p[0],

    如果執行 p[0]

  • that will evaluate to the first element of p,

    就表示要取得 p 的第 1 個元素

  • which is the string containing the single letter y.

    這裡就是含有單個字母 y 的字串

  • With strings, we saw that we could use the colon inside the square brackets

    對於字串,我們可以在中括號裡使用冒號

  • to select a sub string of more than 1 character.

    來選擇長度超過一個字元的部分字串

  • Here we're selecting from position 2 through position 4.

    這裡我們選擇位置 2 到位置 4

  • That will give us the third and fourth characters of the string,

    會得到字串的第 3 和第 4 個字元

  • which is the sub sequence, the string "bb."

    也就是次序列 'bb'

  • We can do the same thing with lists.

    對於列表,我們可以進行相同的操作

  • We can select from position 2 to position 4,

    我們可以選擇位置 2 到位置 4

  • but instead of returning a string, it will return a list

    但得到的不是字串

  • containing those elements.

    而是包含這些元素的列表

  • It will give us a list of the third and fourth element

    我們得到變數 p 的第 3 、第 4 元素的列表

  • of the variable p, which is the list that we have here.

    就是這個列表

  • The general grammar for constructing a list

    建構列表的通用文法

  • is to have a square bracket followed by a list

    是使用中括號

  • of any number of expressions where the expressions

    後面跟著一系列任何數量的運算式

  • are separated by commas.

    由逗點將其分開

  • We could create a list using just 2 brackets,

    我們可以只用一對括號創立一個列表

  • a left bracket and a right bracket, and this would create a list

    左括號和右括號

  • containing 0 elements, also known as the empty list.

    這會建立包含 0 個元素的列表,[br]也稱為空列表 (empty list)

  • We could create a list containing 1 element.

    我們可以建立包含 1 個元素的列表

  • That would be the square brackets with 1 element between them.

    也就是中括號裡有一個元素

  • Here we've created a list containing just 1 element,

    現在我們建立了包含 1 個元素的列表

  • which is the number 3.

    數字 3

  • Or we could create a list with many elements, as we did in the first example,

    我們也可以建立包含許多元素的列表,就像第一個例子

  • where we have all of the strings separated by commas.

    所有的字串被逗點分開

Welcome back for Unit 3.

歡迎回到第三單元

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋