List
List變數是可變的(mutable)
官方文件
1. 操作
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)
參看PEP8
if not seq:
if seq:
2.方法
操作在原陣列上進行,不像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.
3.資料結構
# 佇列
from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry") # Terry arrives
queue.popleft() # The first to arrive now leaves
4.列表推導 List Comprehensions
[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
String變數是不可變的(immutable)
`doesn`t` # 等價 "doesn`t"
print(`C:some
ame`) # C:some ame
print(r`C:some
ame`) # r表示raw, C:some
ame
# 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的區別。
a = `€20`.encode(`utf-8`)
b = b`xe2x82xac20`.decode(`utf-8`)
1.方法
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 abc`.expandtabs() # len = 8 + 3, 補足八位
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
de fg
kl
`.splitlines() # [`ab c`, ``, `de fg`, `kl`]
`www.example.com`.strip(`cmowz.`) # The outermost leading and trailing chars argument values are stripped from the string
2.format方法
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 }}.
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)
3.Template strings
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
numbers.Number
The numbers module (PEP 3141) defines a hierarchy of numeric abstract base classes.
Number :> Complex :> Real :> Rational :> Integral, where A :> B means “A is a supertype of B”.
Python supports arbitrarily large integers.
Bytes,Bytearray
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``
Tuple
Tuples are immutable
t = (12345, 54321, `hello!`)
t[0] # 12345
Set
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`}
Dictionary
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)