import sys
sys.getsizeof([1,2,3])
In [1]: %timeit l = [1,2,3,4,5,6,7,8,9,0] 58.1 ns ± 1.42 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) In [2]: %timeit l = (1,2,3,4,5,6,7,8,9,0) 9.78 ns ± 0.114 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
Dict 原理:[Algorithm] Hashing for searchhtml
Bisect 模块:一个有趣的python排序模块:bisectpython
Tim 排序:[Algorithm] Sort for Fun!算法
一来效率高;二来支持多列表。注意,解开”嵌套“的顺序。编程
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flat = [x for row in matrix for x in row] print(flat)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
filtered = [[x for x in row if x % 3 == 0] for row in matrix if sum(row) >= 10]
print(filtered)
>>> [[6], [9]]
特例:初始化字符串api
>>> sList = list("hello") >>> sList ['h', 'e', 'l', 'l', 'o']
append # 添加一个元素
pop # 拿走一个元素
sort
reverse
In [11]: dir(list) Out[11]: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
与apend的区别是:extend只做用于List。app
>>> L = [1, 2] >>> M = L >>> L += [3, 4] # 仍是原来的对象,只是变大了 >>> L, M # M sees the in-place change too! ([1, 2, 3, 4], [1, 2, 3, 4])
>>> L = [1, 2] >>> M = L # L and M reference the same object >>> L = L + [3, 4] # 实际上是新对象 >>> L, M # Changes L but not M ([1, 2, 3, 4], [1, 2])
第一个变了;第二个没变,觉得 [:] 表明了‘拷贝’ 的意思。ssh
经过地址查看ide
>>> from collections import Iterable >>> isinstance('abc', Iterable) # str是否可迭代 True >>> isinstance([1,2,3], Iterable) # list是否可迭代 True >>> isinstance(123, Iterable) # 整数是否可迭代 False
[ 某行第一个元素 for 某行 in 矩阵 ]函数
提取其中一列columnoop
>>> col2 = [row[1] for row in M] # Collect the items in column 2
>>> col2 [2, 5, 8] >>> M # The matrix is unchanged
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [row[1] for row in M if row[1] % 2 == 0] # Filter out odd items
[2, 8]
有点pipeline的意思
>>> [[x ** 2, x ** 3] for x in range(4)] # Multiple values, "if" filters
[[0, 0], [1, 1], [4, 8], [9, 27]]
>>> [[x, x / 2, x * 2] for x in range(−6, 7, 2) if x > 0] [[2, 1, 4], [4, 2, 8], [6, 3, 12]]
map() 会根据提供的函数对"指定序列"作映射。
map(function, iterable, ...)
# 1. 独立函数
>>>def square(x) : # 计算平方数 ... return x ** 2 ... >>> map(square, [1,2,3,4,5]) # 计算列表和:1+2+3+4+5 [1, 4, 9, 16, 25]
----------------------------------------------------------------
# 2. 匿名函数 >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数 [1, 4, 9, 16, 25]
---------------------------------------------------------------- # 3. 提供了两个列表,对相同位置的列表数据进行相加 >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19]
Jeff: "第三个也是相对于 for 而言的一个优点"
计算后获得若干离散值。
少了 "index定位" 这样的事情。
In [13]: M = [[1, 2, 3], # A 3 × 3 matrix, as nested lists ...: ...: [4, 5, 6], # Code can span lines if bracketed ...: ...: [7, 8, 9]]
In [14]: G = (sum(row) for row in M) # <---- In [16]: next(G) # 输出一行 Out[16]: 6 In [17]: next(G) # 再输出一行 Out[17]: 15 In [18]: next(G) # 再输出一行 Out[18]: 24
列表(方括号),集合(大括号),字典(大括号),元组(圆括号) 效果对比:
直接自定义了若干离散值。
yield x: Generator function send protocol
From: https://www.jianshu.com/p/d09778f4e055
带有 yield 的函数再也不是一个普通函数,而是一个生成器generator,可用于迭代,工做原理同next()。
相似 return 的关键字。
send(msg)与next()的区别在于send能够传递参数给yield表达式,这时传递的参数会做为yield表达式的值,而yield的参数是返回给调用者的值。
其实就是让一个函数分步执行:
>>> def get_0_1_2():
... yield 0 ... yield 1 ... yield 2 ... >>> get_0_1_2 <function get_0_1_2 at 0x00B2CB70>
>>> generator.next()
generator = get_0_1_2() # 绑定了函数后就开始执行
>>> generator.next()
0
>>> generator.next() 1 >>> generator.next() 2
api有了稍许变化!【这个貌似好用】
generator = get_0_1_2() # 必须这么绑定一下,直接用函数名不行
In [83]: next(generator) Out[83]: 0 In [84]: next(generator) Out[84]: 1 In [85]: next(generator) Out[85]: 2 In [86]: next(generator)
Error.
改变了本身自己。
>>> L = ['abc', 'ABD', 'aBe'] >>> L.sort() # Sort with mixed case
>>> L ['ABD', 'aBe', 'abc']
>>> L = ['abc', 'ABD', 'aBe'] >>> L.sort(key=str.lower) # Normalize to lowercase 忽略大小写
>>> L ['abc', 'ABD', 'aBe']
>>> L = ['abc', 'ABD', 'aBe'] >>> L.sort(key=str.lower, reverse=True) # Change sort order 反过来
>>> L ['aBe', 'ABD', 'abc']
生成了新的列表。
>>> L = ['abc', 'ABD', 'aBe'] >>> sorted(L, key=str.lower, reverse=True) # Sorting built-in
['aBe', 'ABD', 'abc']
>>> L = ['abc', 'ABD', 'aBe'] >>> sorted([x.lower() for x in L], reverse=True) # Pretransform items: differs!
['abe', 'abd', 'abc']
>>> D = {'spam': 2, 'ham': 1, 'eggs': 3} # 显式初始化
>>> bob1 = dict(name='Bob', job='dev', age=40) # 参数初始化 >>> bob1 {'age': 40, 'name': 'Bob', 'job': 'dev'}
-----------------------------------------------------------------------
------------------------------ 数字 -----------------------------------
-----------------------------------------------------------------------
>>> D = dict.fromkeys(['a', 'b', 'c'], 0) # Initialize dict from keys >>> D {'b': 0, 'c': 0, 'a': 0}
----------------------------------------------------------------------- >>> D = {k:0 for k in ['a', 'b', 'c']} # Same, but with a comprehension >>> D {'b': 0, 'c': 0, 'a': 0}
-----------------------------------------------------------------------
------------------------------ 字符串 ----------------------------------
-----------------------------------------------------------------------
>>> D = dict.fromkeys('spam') # Other iterables, default value >>> D {'s': None, 'p': None, 'a': None, 'm': None}
----------------------------------------------------------------------- >>> D = {k: None for k in 'spam'} >>> D {'s': None, 'p': None, 'a': None, 'm': None}
>>> bob2 = dict( zip(['name', 'job', 'age'], ['Bob', 'dev', 40]) ) # Zipping >>> bob2 {'job': 'dev', 'name': 'Bob', 'age': 40}
zip的中间过程展现:
>>> list( zip(['a', 'b', 'c'], [1, 2, 3]) ) # Zip together keys and values [('a', 1), ('b', 2), ('c', 3)]
>>> D = dict( zip(['a', 'b', 'c'], [1, 2, 3]) ) # Make a dict from zip result >>> D {'b': 2, 'c': 3, 'a': 1}
# 进一步,在配对的过程当中能够作一些lamdb的操做
>>> D = {k: v for (k, v) in zip(['a', 'b', 'c'], [1, 2, 3])}
>>> D
{'b': 2, 'c': 3, 'a': 1}
zip的相反操做 单星号,以下:
>>> a = [1,2,3] >>> b = [4,5,6] >>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) # 打包为元组的列表 [(1, 4), (2, 5), (3, 6)]
>>> zip(a,c) # 元素个数与最短的列表一致 [(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped) # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式 [(1, 2, 3), (4, 5, 6)]
a = {‘age’: 23, ‘name’: ‘lala} a[school] = ‘nanhaizhongxue’ print a >>> {‘age’: 23, ‘name’: ‘lala’, ‘school’: ‘nanhaizhongxue’}
>>> D {'eggs': 3, 'spam': 2, 'ham': 1}
>>> D2 = {'toast':4, 'muffin':5} # Lots of delicious scrambled order here
>>> D.update(D2) >>> D {'eggs': 3, 'muffin': 5, 'toast': 4, 'spam': 2, 'ham': 1}
单独输出全部的key;单独输出全部的value;单独输出全部的(key, value);
print(dic.keys()) # dict_keys(['赵四', '刘能', '王木生']) 像列表. 山寨列表 for k in dic.keys(): # 拿到的是字典中的每个key print(k)
print(dic.values()) # dict_values(['刘晓光', '王晓利', '范伟']) 全部的value的一个数据集 for v in dic.values(): print(v)
print(dic.items()) # 全部的键值对 dict_items([('赵四', '刘晓光'), ('刘能', '王晓利'), ('王木生', '范伟')]) for k, v in dic.items(): # 遍历字典最简单的方案 print(item) # ('赵四', '刘晓光') k, v = item # 解构 k = item[0] v = item[1] print(k, v)
默认的是直接遍历key值。
dic = {"赵四":"刘晓光", "刘能":"王晓利", "王木生":"范伟"} # 直接for循环 for key in dic: # 直接循环字典拿到的是key, 有key直接拿value print(key) print(dic[key])
获取 value 的第二种方式
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 27} print "Value : %s" % dict.get('Age') print "Value : %s" % dict.get('Sex', "Never")
先取出key值,再排序。
>>> Ks = list( D.keys() ) # Unordered keys list >>> Ks # A list in 2.X, "view" in 3.X: use list() ['a', 'c', 'b']
>>> Ks.sort() # Sorted keys list >>> Ks ['a', 'b', 'c']
>>> for key in Ks: # Iterate though sorted keys print(key, '=>', D[key]) # <== press Enter twice here (3.X print) a => 1 b => 2 c => 3
默认是排序value值。
# 键 >>> list( D.items() ) [('eggs', 3), ('spam', 2), ('ham', 1)]
第一种方法:使用自带函数实现:
在 python 的字典的属性方法里面有一个 has_key() 方法:
#生成一个字典 d = {'name':Tom, 'age':10, 'Tel':110}
#打印返回值 print d.has_key('name') #结果返回True
第二种方法:使用 in 方法: 【推荐,更快】
#生成一个字典
d = {'name':'Tom', 'age':10, 'Tel':110} #打印返回值,其中d.keys()是列出字典全部的key,如下两个结果同样,返回True
print(‘name’ in d.keys()) print('name' in d) #一个例子:多维数据使用 dict.
>>> if (2, 3, 6) in Matrix: # Check for key before fetch
... print(Matrix[(2, 3, 6)]) # See Chapters 10 and 12 for if/else
... else: ... print(0) ... 0
除了使用 in 还能够使用 not in。
第三种方法:try...except方法:
若是不在,形成错误,大不了走except路线。
>>> try: ... print(Matrix[(2, 3, 6)]) # Try to index
... except KeyError: # Catch and recover
... print(0) # See Chapters 10 and 34 for try/except
... 0
(1) map
>>> def f(x): ... return x * x ... >>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> list(r) [1, 4, 9, 16, 25, 36, 49, 64, 81]
(2) reduce
>>> from functools import reduce >>> def fn(x, y): ... return x * 10 + y ... >>> reduce(fn, [1, 3, 5, 7, 9]) 13579
(3) map + reduce
from functools import reduce DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return DIGITS[s] return reduce(fn, map(char2num, s))
>>> Matrix = {} >>> Matrix[(2, 3, 4)] = 88 >>> Matrix[(7, 8, 9)] = 99 >>> >>> X = 2; Y = 3; Z = 4 # ; separates statements: see Chapter 10 这里更灵活! >>> Matrix[(X, Y, Z)] 88
携带一些比较相似list的性质,但功能较少。
>>> T.index(4) # Tuple methods: 4 appears at offset 3 3 >>> T.count(4) # 4 appears once 1
Why Lists and Tuples?:
Frankly, tuples are not generally used as often as lists in practice, but their immutability is the whole point.
If you pass a collection of objects around your program as a list, it can be changed anywhere; if you use a tuple, it cannot.
不变性,可能就是其存在的意义。
括号()
既能够表示tuple,又能够表示数学公式中的小括号。
只有一个元素的tuple必须跟着“逗号”
>>> t = (1)
>>> t 1 >>> t = (1,) >>> t (1,)
tuple的第一级元素不能变,但控制不了元素内部的“可变”。
>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t ('a', 'b', ['X', 'Y'])
>>> X = set('spam') # Make a set out of a sequence in 2.X and 3.X >>> Y = {'h', 'a', 'm'} # Make a set with set literals in 3.X and 2.7
>>> X, Y # A tuple of two sets without parentheses ({'m', 'a', 'p', 's'}, {'m', 'a', 'h'})
>>> X & Y # Intersection {'m', 'a'}
>>> X | Y # Union {'m', 'h', 'a', 'p', 's'}
>>> X - Y # Difference {'p', 's'}
>>> X > Y # Superset False
注意,这里是大括号。
>>> {n ** 2 for n in [1, 2, 3, 4]} # Set comprehensions in 3.X and 2.7 {16, 1, 4, 9}
Goto: Python列表、元组、集合、字典的区别和相互转换
End.