List變量是可變的(mutable)
官方文檔python
squares = [1, 4, 9, 16, 25] squares[-1] # 25 squares[-3:] # returns a new list [9, 16, 25] squares[1:3] # returns a new list [4, 9] squares[:] # returns a new (shallow) copy [1, 4, 9, 16, 25] squares + [36, 49, 64, 81, 100] # concatenation, return a new list squares[0] = 0 # replaces the value squares[len(squares):] = [36] # appends a '36' squares[1:3] = [] # removes [4, 9] squares[:] = [] # clears the list -> del squares[:] squares *= 2 # 元素重複2次,[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]
判斷list是否爲空:不要用len(seq)
參看PEP8git
if not seq: if seq:
操做在原數組上進行,不像concatenation同樣返回新數組數組
sum([1,2,3,4]) # 10 max([1,2,3,4]) # 4 list.append(x) # equivalent to a[len(a):] = [x], x是元素 list.extend(iterable) # equivalent to a[len(a):] = iterable, iterable是數組 list.remove(x) # x是元素 list.pop([i]) # 默認刪除最後一個元素, i表示index, []表示可選 list.clear() list.index(x[, start[, end]]) # 返回元素x的index list.count(x) # 返回元素x個數 list.sort(key=None, reverse=False) ''' "key=" specifying a "key" function that transforms each element before comparison. The key function takes in 1 value and returns 1 value, and the returned "proxy" value is used for the comparisons within the sort. ''' list.sort(key=len) # 先獲得每一個元素的len,而後根據len排序 list.reverse() list.copy() # Return a shallow copy of the list. Equivalent to a[:]
You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.網絡
# 隊列 from collections import deque queue = deque(["Eric", "John", "Michael"]) queue.append("Terry") # Terry arrives queue.popleft() # The first to arrive now leaves
[x**2 for x in range(10)] [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] # 注意for、if的順序 combs = [] for x in [1,2,3]: for y in [3,1,4]: if x != y: combs.append((x, y)) >>>combs
# flatten a list vec = [[1,2,3], [4,5,6], [7,8,9]] [num for elem in vec for num in elem] flatten = [] for elem in vec: for num in elem: flatten.append(num) >>>flatten
# 矩陣倒置 # 可是最好使用list(zip(*matrix)) matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ] [[row[i] for row in matrix] for i in range(4)] # !!! 變量使用以前要像這樣聲明一遍 transposed = [] for i in range(4): transposed.append([row[i] for row in matrix]) >>> transposed transposed = [] for i in range(4): # the following 3 lines implement the nested listcomp transposed_row = [] for row in matrix: transposed_row.append(row[i]) transposed.append(transposed_row) >>> transposed
String變量是不可變的(immutable)數據結構
'doesn\'t' # 等價 "doesn't" print('C:\some\name') # C:\some ame print(r'C:\some\name') # r表示raw, C:\some\name # print多行 print("""\ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """) 3 * 'un' + 'ium' # 'unununium' 'Py' 'thon' # 'Python', 很是有用,用於分解多行 text = ('Put several strings within parentheses ' 'to have them joined together.') # 以上方法,不能用於變量(或表達式) 與 字符串的鏈接。須要使用+
String 與 bytes 區別:
\o12 八進制,\x0a十六進制。但,像單獨一個'a',既能夠是十六進制,也能夠是八進制,無須轉義
通常狀況,咱們無須關注底層一個字符花費多少byte。只有在須要將string編碼(encode)成byte的時候,好比:經過網絡傳輸數據;或者須要將byte解碼(decode)成string的時候,咱們纔會關注string和byte的區別。app
a = '€20'.encode('utf-8') b = b'\xe2\x82\xac20'.decode('utf-8')
s = 'abc' s.center(10, '@') # @@@abc@@@@ s.count('a') # 1 s.encode('utf-8') # b'abc' s.endswith('b') # False s.startswith(('a','A')) # 傳入tuple,用於查詢 '1,2,3,4'.split(',', 2) # ['1', '2', '3,4'] 'abc\tabc'.expandtabs() # len = 8 + 3,\t補足八位 s.find('b') # retuen the lowest index. 注意:老是用'b' in s來判斷是不是子串 s.rfind('b') # retuen the highest index. "The jack is {1}".format(1, 2, 3) "The jack is {1}".format(*[1, 2, 3]) "The jack is {jack}".format(jack=4098, sape=4139) "The jack is {jack}".format(**{'jack': 4098, 'sape': 4139}) s.index('d') # 等同於find;raise ValueError s.join('de') # return a new sttring 'abcde' s.ljust(5, '@') # abc@@ 補充右側 s.rjust(5, '@') # @@abc 補充左側 s.strip('ab') s.lstrip('123a') # specifying the set of characters to be removed; If none para, removing whitespace s.rstrip('123c') # 'ab' s.partition('b') # return a tuple ('a', 'b', 'c'),第一個出現的b s.rpartition('b') # return a tuple ('a', 'b', 'c'),最後一個出現的b s.replace('a','A') # replace(old, new[, count]) 替代第count個old字符串替換爲new,count從1開始 'ab c\n\nde fg\rkl\r\n'.splitlines() # ['ab c', '', 'de fg', 'kl'] 'www.example.com'.strip('cmowz.') # The outermost leading and trailing chars argument values are stripped from the string
Mostly used to combine Number with String, e.g. 'Key1'
Format strings contain 「replacement fields」 surrounded by curly braces {}. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.curl
class a: def __init__(self): self._x = 10 self._y = 45 a = a() 'y is {0._y}, x is {0._x}'.format(a) # y is 45, x is 10 b = [45, 10] 'y is {0[0]}, x is {0[1]}'.format(b) 'y is {arg[0]}, x is {arg[1]}'.format(arg = b) 'array is {0!r}'.format(b) # array is [45, 10] # Three conversion flags : '!s' which calls str() on the value, #'!r' which calls repr() and '!a' which calls ascii(). 'int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}'.format(42) # 只接受數字 # 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' points = 19 total = 22 'Correct answers: {:.2%}'.format(points/total) # 86.36% import datetime d = datetime.datetime(2010, 7, 4, 12, 15, 58) '{:%Y-%m-%d %H:%M:%S}'.format(d)
from string import Template t = Template('$who likes $whom') t.substitute(who='Jam', whom='Mo') # Jam likes Mo d = dict(who='Jam', whom='Mo') t.substitute(d) t.safe_substitute(who='Jam') # Jam likes $whom
The numbers module (PEP 3141) defines a hierarchy of numeric abstract base classes.ui
Number :> Complex :> Real :> Rational :> Integral, where A :> B means "A is a supertype of B".this
Python supports arbitrarily large integers.
byte對象是不可變的(immutable)
bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256
Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal numbers are a commonly used format for describing binary data
bytearray objects are a mutable counterpart to bytes objects
b = b'€20' # SyntaxError bytes.fromhex('30') # b'0', fromhex()類方法class method bytes.fromhex('303132') # b'012',十六進制 bytes([50,51,52]) # b'234',十進制。避免使用此法實例bytes,用b''
Tuples are immutable
t = (12345, 54321, 'hello!') t[0] # 12345
A set is an unordered collection with no duplicate elements
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} 'orange' in basket a = set('abracadabra') b = set('alacazam') a-b # letters in a but not in b a|b # letters in a or b or both a&b # letters in both a and b a^b # letters in a or b but not both {x for x in 'abracadabra' if x not in 'abc'}
tel = {'jack': 4098, 'sape': 4139} tel.keys() tel.values() tel['guido'] = 4127 'guido' in tel # True 'guido' in tel.keys() # 不建議,keys方法會建立返回key數組,下降效率 {x: x**2 for x in (2, 4, 6)} d = dict(sape=4139, guido=4127, jack=4098) for k, v in d.items(): print(k, v)