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\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的区别。
a = '€20'.encode('utf-8')
b = b'\xe2\x82\xac20'.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\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
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)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。