|
查看内置函数: print(dir(__builtins__))
常见函数:
len 求长度
min 最小值
max 最大值
sorted 排序,从小到大
reversed 反向
sum 求和
进制转换:
bin() 转换为二进制
oct() 转换为八进制
hex() 转换为十六进制
ord() 将字符转换成对应的ASIIC码值
chr() 将ASIIC码值转换成对应的字符
补充:
1.enumerate() 返回一个可以枚举的对象
2.filter() 过滤器
3.map() 加工。对于参数iterable中的每个元素都应用fuction函数,并返回一个map对象
4.zip() 将对象逐一配对
1.1 查看参数使用:
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, start=0, /)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
>>> sum((1,23,4))
28
>>> sum([1,2,3])
6
>>> sum([1,2,3],10)
16
>>> sum([10,20,30],20) #值=iterable值+start值
80
>>> sum([10,20,30],22) #值=iterable值+start值
82
>>> sum({1:12,2:30}) #key相加
3
1.2 二进制:
>>> bin(1)
'0b1'
>>> bin(2)
'0b10'
1.3 八进制:
>>> oct(8)
'0o10'
>>> oct(12)
'0o14'
1.4 十六进制:
>>> hex(10)
'0xa'
>>> hex(9)
'0x9'
>>> hex(15)
'0xf'
1.5 将ASIIC码转换成相应的字符
>>> chr(65)
'A'
>>> chr(32)
' '
1.6 将字符转换成ASIIC码
>>> ord('a')
97
>>> ord(' ')
32
1.7 enumerate:
>>> help(enumerate)
Help on> class enumerate(object)
| enumerate(iterable[, start]) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be another object that supports
| iteration. The enumerate object yields pairs containing a count (from
| start, which defaults to zero) and a value yielded by the iterable argument.
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
| Implement iter(self).
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __next__(self, /)
| Implement next(self).
|
| __reduce__(...)
| Return state information for pickling.
>>> enumerate([1,2,3,4])
<enumerate object at 0x000000000343EF78> #返回一个迭代器
>>> list(enumerate([1,2,3,4])) #查看
[(0, 1), (1, 2), (2, 3), (3, 4)] #返回一个带索引的可枚举对象,index默认0,也可指定
>>> list(enumerate(['a','b','c','d']))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] #返回一个带索引的可枚举对象,index默认0,也可指定
>>> list(enumerate(['a','b','c','d'],3))
[(3, 'a'), (4, 'b'), (5, 'c'), (6, 'd')]
>>> list(enumerate((1,23,4,5,6),3))
[(3, 1), (4, 23), (5, 4), (6, 5), (7, 6)]
>>> list(enumerate({1,2,3,4,5},3)) #返回一个伪索引
[(3, 1), (4, 2), (5, 3), (6, 4), (7, 5)]
>>> list(enumerate({1:2,2:3,3:4},3)) #按可以返回
[(3, 1), (4, 2), (5, 3)]
1.8 filter 过滤器
>>> help(filter)
Help on> class filter(object)
| filter(function or None, iterable) --> filter object
| Return an iterator yielding those items of iterable for which function(item)
| is true. If function is None, return the items that are true.
| Methods defined here:
| __getattribute__(self, name, /)
| Return getattr(self, name).
| __iter__(self, /)
| Implement iter(self).
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
| __next__(self, /)
| Implement next(self).
| __reduce__(...)
| Return state information for pickling.
>>> filter(lambda x:x>2,[1,2,3,4,5]) #lambda x:x>2是个函数体
<filter object at 0x0000000003420EB8> #返回函数体
>>> list(filter(lambda x:x>2,[1,2,3,4,5]))
[3, 4, 5]
>>> list(filter(None,[1,2,3,4,5]))
[1, 2, 3, 4, 5]
1.9 map加工
>>> help(map)
Help on> class map(object)
| map(func, *iterables) --> map object
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
| Methods defined here:
| __getattribute__(self, name, /)
| Return getattr(self, name).
| __iter__(self, /)
| Implement iter(self).
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
| __next__(self, /)
| Implement next(self).
| __reduce__(...)
| Return state information for pickling.
>>> list(map(str,[1,2,3,4]))
['1', '2', '3', '4']
1.10 zip 将对象逐一配对
>>> list(zip([1,2,3],[4,5,6]))
[(1, 4), (2, 5), (3, 6)]
>>> list(zip((1,2,3),(4,5,6)))
[(1, 4), (2, 5), (3, 6)]
>>> list(zip([1,2,3],(4,5,6)))
[(1, 4), (2, 5), (3, 6)]
>>> list(zip([1,2,3,4],[5,6,7,8,9],['a','b']))
[(1, 5, 'a'), (2, 6, 'b')] |
|