1. 程式人生 > >Python3基礎——序列類型

Python3基礎——序列類型

nat copy operation after max not 下標 元組 rabl

開頭寫給自己,To Myself:

很久以來,都想要學習一門編程語言,從去年選擇了python開始,反反復復重新開始了N多遍,每一次不會超過倆星期。昨天無意間翻開自己去年記的學習筆記,不禁感嘆想當年我曾那麽用功,卻未能堅持下來,著實可惜。這一次,我告訴自己,最後一次機會,必須堅持到底!must。。。。。。

Sequence Types — list, tuple, range

Common Sequence Operations

OperationResultNotes
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

OperationResultNotes
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基礎——序列類型