標籤:mutable example body clear 逗號 singleton create sort 關係
開頭寫給自己,To Myself:
很久以來,都想要學習一門程式設計語言,從去年選擇了python開始,反反覆複重新開始了N多遍,每一次不會超過倆星期。昨天無意間翻開自己去年記的學習筆記,不禁感歎想當年我曾那麼用功,卻未能堅持下來,著實可惜。這一次,我告訴自己,最後一次機會,必須堅持到底!must。。。。。。
Sequence Types — list, tuple, range
Common Sequence Operations
| Operation |
Result |
Notes |
| x in s |
True if an item of s is equal to x, else False——關係操作符 |
(1) |
| x not in s |
False if an item of s is equal to x, else True——關係操作符 |
(1) |
| s + t |
the concatenation of s and t——串連操作符 |
(6)(7) |
| s * n or n * s |
equivalent to adding s to itself n times——重複操作符 |
(2)(7) |
| s[i] |
ith item of s, origin 0——擷取下標位i的元素,下標從0開始 |
(3) |
| s[i:j] |
slice of s from i to j——切片拷貝 |
(3)(4) |
| s[i:j:k] |
slice of s from i to j with step k |
(3)(5) |
| len(s) |
length of s |
|
| min(s) |
smallest item of s |
|
| max(s) |
largest item of s |
|
| s.index(x[, i[, j]]) |
index of the first occurrence of x in s (at or after index i and before index j)
元素x在s中最早出現的位置下標,可以設定下標所在範圍的起始位置 |
(8) |
| s.count(x) |
total number of occurrences of x in s——元素x在序列s中出現的次數 |
|
Immutable Sequence Types
hash()
Mutable Sequence Types
| Operation |
Result |
Notes |
| s[i] = x |
item i of s is replaced by x |
|
| s[i:j] = t |
slice of s from i to j is replaced by the contents of the iterable t |
|
| del s[i:j] |
same as s[i:j] = [] |
|
| s[i:j:k] = t |
the elements of s[i:j:k] are replaced by those of t |
(1) |
| del s[i:j:k] |
removes the elements of s[i:j:k] from the list |
|
| s.append(x) |
appends x to the end of the sequence (same as s[len(s):len(s)] = [x]) |
|
| s.clear() |
removes all items from s (same as del s[:]) |
(5) |
| s.copy() |
creates a shallow copy of s (same as s[:]) |
(5) |
| s.extend(t) or s += t |
extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t) |
|
| s *= n |
updates s with its contents repeated n times |
(6) |
| s.insert(i, x) |
inserts x into s at the index given by i (same as s[i:i] = [x]) |
|
| s.pop([i]) |
retrieves the item at i and also removes it from s |
(2) |
| s.remove(x) |
remove the first item from s where s[i] == x |
(3) |
| s.reverse() |
reverses the items of s in place |
(4) |
Lists
1、建立列表的方法:
- Using a pair of square brackets to denote the empty list: []
- Using square brackets, separating items with commas: [a], [a, b, c]
- Using a list comprehension: [x for x in iterable]
- Using the type constructor: list() or list(iterable)
list(‘abc‘) returns [‘a‘, ‘b‘, ‘c‘]
list( (1, 2, 3) ) returns [1, 2, 3]
2、除了以上方法外,列表還支援sort()方法
sort(*, key=None, reverse=None)
對列表進行排序,預設是按照從小到大的順序排列
Tuples
1、建立元組的方法:
- Using a pair of parentheses to denote the empty tuple: ()
- Using a trailing comma for a singleton tuple: a, or (a,)
- Separating items with commas: a, b, c or (a, b, c)
- Using the tuple() built-in: tuple() or tuple(iterable)
For example, tuple(‘abc‘) returns (‘a‘, ‘b‘, ‘c‘) and tuple( [1, 2, 3] ) returns (1, 2, 3).
注意:當你要建立的元組只有一個元素時,必須帶逗號
Ranges
代表一組不可變的數字序列,主要用於for迴圈
class range(stop)
class range(start, stop[, step])
參數必須為整數,step步幅預設為1,start開始參數預設為0、
>>> list(range(10))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> list(range(1, 11))[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> list(range(0, 30, 5))[0, 5, 10, 15, 20, 25]>>> list(range(0, 10, 3))[0, 3, 6, 9]>>> list(range(0, -10, -1))[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]>>> list(range(0))[]>>> list(range(1, 0))[]
Python3基礎——序列類型