Python3 学习笔记

Python_8_Python内建函数-迭代器

2019-05-28  本文已影响0人  静堂先生

1. 内建函数 1

In : a = 123

In : id(a)
Out: 9330656

In : def b(): 
...:     return a 
...:

In : id(b)
Out: 139701663500624

In : b()
Out: 123
In : hash(range(3))
Out: 5050907061201647097

In : hash([3])    # 不能 hash 可变对象
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-b1bd0fbc0b77> in <module>
----> 1 hash([3])

TypeError: unhashable type: 'list'
In : type(range(3))
Out: range

In : type(3)
Out: int

In : type(lambda x: x+1)
Out: function

输入 input([prompt])

In : input('please input: ')
please input: hello
Out: 'hello'

打印 print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

In : print('hello noob')
hello noob

In : print('abc', 'hello', 'ok', sep='\n')
abc
hello
ok

2. 内建函数 2

对象长度 len(s)

In : lst = [i for i in range(9)]

In : tup = (1, 2, 3)

In : st = 'colinlee'  

In : len(lst), len(tup), len(st)
Out: (9, 3, 8)

isinstance(obj, class_or_tuple)

In : isinstance(lst, tuple)  
Out: False

In : isinstance(tup, tuple)  
Out: True

In : isinstance(st, str)  
Out: True

In : isinstance(0, bool)  
Out: False

In : isinstance(True, int)  
Out: True

issubclass(cls, class_or_tuple)

In : issubclass(bool, int)  
Out: True

In : issubclass(float, int)  
Out: False

3. 内建函数 3

绝对值 abs(x) (x 为数值)

In : abs(-3)
Out: 3

In : abs(-3.14)  
Out: 3.14

In : abs(5.20)  
Out: 5.2

最大值 max() 最小值 min()

In : max(1, 3, 1, 4)  
Out: 4

In : min(1, 3, 1, 4)  
Out: 1

In : import random  
In : sam = random.sample(range(10), k=6)  
In : sam  
Out: [4, 1, 0, 3, 6, 9]

In : max(sam), min(sam)  
Out: (9, 0)

round(x) 四舍六入五取偶,round(-0.5)

In : round(1.4), round(1.6), round(1.5)
Out: (1, 2, 2)

In : round(-1.4), round(-1.6), round(-1.5)
Out: (-1, -2, -2)

In : round(-0.4), round(-0.6), round(-0.5)
Out: (0, -1, 0)

pow(x , y) 等价于 x**y

In : pow(2, 3), pow(2, 0)
Out: (8, 1)

range(stop) 从 0 开始到 stop-1 的可迭代对象;range(start, stop [, step]) 从 start 开始到 stop-1 结束步长为 step 的可迭代对象

In : range(3)
Out: range(0, 3)

In : print(range(3))
range(0, 3)

In : for i in range(3):
    ...:     print(i)
    ...:
0
1
2

In : for i in range(1, 10, 2):
    ...:     print(i)
    ...:
1
3
5
7
9

divmod(x, y) 等价于 tuple([x//y, x%y])

In : divmod(10, 3)
Out: (3, 1)

In : tuple(10//3, 10%3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-d1b3fe479f67> in <module>
----> 1 tuple(10//3, 10%3)

TypeError: tuple() takes at most 1 argument (2 given)

In : tuple([10//3, 10%3])
Out: (3, 1)

sum(iterable [, start]) 对可迭代对象的所有数值元素求和

In : sum(range(1,101,1))
Out: 5050

4. 内建函数 4

In : chr(97), chr(20013)
Out: ('a', '中')
In : ord('a'), ord('中')
Out: (97, 20013)

str() 返回 object 的 字符串 版本。 如果未提供 object 则返回空字符串。

In : str(123)
Out: '123'

In : a = 'good'

In : str(a)
Out: 'good'

repr() 返回包含一个对象的可打印表示形式的字符串。

In : repr(123)
Out: '123'

In : repr(range(3))
Out: 'range(0, 3)'

In : class cls:
    ...:     pass
    ...:

In : repr(cls)
Out: "<class '__main__.cls'>"

ascii() 就像函数 repr(),返回一个对象可打印的字符串,但是 repr() 返回的字符串中非 ASCII 编码的字符,会使用 \x、\u 和 \U 来转义。生成的字符串和 Python 2 的 repr() 返回的结果相似。

In : st = 'I \tam \nColinLee'

In : print(st)
I       am
ColinLee

In : ascii(st)
Out: "'I \\tam \\nColinLee'"

In : ascii('胜利是属于我们的')
Out: "'\\u80dc\\u5229\\u662f\\u5c5e\\u4e8e\\u6211\\u4eec\\u7684'"

5. 内建函数 5

sorted(iterable [, key][, reverse]) 排序

In : sorted([1, 3, 5])
Out: [1, 3, 5]

In : sorted([3, 1, 5])
Out: [1, 3, 5]

In : sorted([1, 3, 5], reverse=True)
Out: [5, 3, 1]

In : dic = {'c':1, 'b':2, 'a':1}

In : sorted(dic)
Out: ['a', 'b', 'c']

In : dic
Out: {'c': 1, 'b': 2, 'a': 1}

6. 内建函数 6

翻转 reversed(seq) 返回一个翻转元素的迭代器

In : list(reversed("13579"))
Out: ['9', '7', '5', '3', '1']

In : {reversed((2, 4)) } # 有几个元素?
Out: {<reversed at 0x210243222b0>}

In : for x in reversed(['c','b','a']): print(x)
a
b
c

In : reversed(sorted({1, 5, 9}))
Out: <list_reverseiterator at 0x21024360748>

In : for x in reversed(sorted({1, 5, 9})): print(x)
9
5
1

7. 内建函数 7

枚举 enumerate(seq, start=0)

In : for x in enumerate([2,4,6,8]): print(x)
(0, 2)
(1, 4)
(2, 6)
(3, 8)

In : for x in enumerate("abcde"): print(x,end=" ")
(0, 'a') (1, 'b') (2, 'c') (3, 'd') (4, 'e')
In : enumerate('abcde')
Out: <enumerate at 0x210242c9c18>

In : print(enumerate('abcde'))
<enumerate object at 0x00000210242C9630>

In : dict(enumerate('abcde', start=1))
Out: {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

8. 内建函数 8

迭代器和取元素 iter(iterable)next(iterator [, default])

In : it = iter(range(3))

In : it
Out: <range_iterator at 0x210242a5670>

In : next(it)
Out: 0

In : next(it)
Out: 1

In : next(it)
Out: 2

In : next(it)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-60-bc1ab118995a> in <module>
----> 1 next(it)

StopIteration:
  
In : it2 = reversed([1,3,5])

In : next(it2)
Out: 5

In : next(it2)
Out: 3

In : next(it2)
Out: 1

In : next(it2)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-65-bc1ab118995a> in <module>
----> 1 next(it2)

StopIteration:

9. 可迭代对象

  ['a', 'a'],虽然这个列表有 2 个元素,值一样,但是两个 'a' 是不同的元素,因为有不同的索引

In : 3 in range(10)
Out: True

In : list(range(10))
Out: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In : 3 in (x for x in range(10))
Out: True

In : (x for x in range(10))
Out: <generator object <genexpr> at 0x0000021023F4B308>

In : tuple(x for x in range(10))
Out: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

In : 3 in {x:y for x,y in zip(range(4),range(4,10))}
Out: True

In : {x:y for x,y in zip(range(4),range(4,10))}
Out: {0: 4, 1: 5, 2: 6, 3: 7}

10. 迭代器

In : for x in iter(range(10)): print(x)
0
1
2
3
4
5
6
7
8
9

In : g = (x for x in range(10))

In : print(type(g))
<class 'generator'>

In : print(next(g))
0

In : print(next(g))
1

In : print(next(g))
2

11. 内建函数 9

拉链函数 zip(*iterables)

In : list(zip(range(10),range(10)))
Out:
[(0, 0),
 (1, 1),
 (2, 2),
 (3, 3),
 (4, 4),
 (5, 5),
 (6, 6),
 (7, 7),
 (8, 8),
 (9, 9)]

In : list(zip(range(10),range(10),range(5),range(10)))
Out: [(0, 0, 0, 0), (1, 1, 1, 1), (2, 2, 2, 2), (3, 3, 3, 3), (4, 4, 4, 4)]

In : dict(zip(range(10),range(10)))
Out: {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}

In : {str(x):y for x,y in zip(range(10),range(1,5))}
Out: {'0': 1, '1': 2, '2': 3, '3': 4}

12. 内建函数 10

all(iterable)

any(iterable)

In : lst = [True, {1}, [2, 3], 5.1, 'abc']

In : print(all(lst), any(lst))
True True

In : print(all([]), any([]))
True False

In : print(all(lst + [0]), any(lst + [0]))
False True

13. 总结

  以上内建函数的权威解释和用法请参见 2. 内置函数 — Python 3.6.8 文档Python3 内置函数 | 菜鸟教程

上一篇 下一篇

猜你喜欢

热点阅读