【python] Python的那些妙事

python 动态创建类

type('Hello', (object,), dict())

set 集合

& 交集
| 并集
^ 补集
- 差集

built-in 函数

  1. breakpoint()
    相当于pdb断点
  2. divmod()
    divmod(4,2) # (2,0)
    divmod(x,y) 可代替 (x//y, x%y)

iter() 迭代器妙用

itr = iter('abcd')
if 'c' in itr:
	print(next(itr))  # d

chain 函数

参数带* 与不带*

import itertools
a= ['a','aa','aaa']
b= itertools.chain(a)
c= itertools.chain(*a)
print(a)
print(list(b))
print(list(c))
# [‘a’, ‘aa’, ‘aaa’]
# [‘a’, ‘aa’, ‘aaa’]
# [‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’]
posted @ 2022-06-22 17:21  倒骑驴子  阅读(21)  评论(0)    收藏  举报