标签:
对上表一些比较重要常用的内置函数做一个整理
chr()与ord() chr()是将ASCII中十进制编号转换成相应的字符,而ord()刚好相反
c = chr(49) o = ord(‘1‘) print(c) print(o) 输出结果: 1 49
知道了chr()的基本用法,可以利用它来生成一个字母验证码,因为验证码都是随机生成的,所以这里要涉及到random模块。在ASCII中,大写字母的十进制编号是从65到90.
小写字母是97到122
1 import random 2 li = [] 3 for i in range(6): 4 r = random.randrange(0,7) 5 if r == 2 or r == 4: 6 num = random.randrange(0,10) 7 li.append(str(num)) 8 elif r ==1 or r == 5: 9 item = random.randrange(97,123) 10 c = chr(item) 11 li.append(c) 12 else: 13 item = random.randrange(65, 91) 14 C = chr(item) 15 li.append(C) 16 result = "".join(li)#将列表的元素提取出来,使用join是,元素需要是字符串类型 17 print(result)
compile() eval() exec() 在了解compile之前,我们先来了解一下python中.py文件是如何打开的。当我们读取文件内容时,把字符串加载至内存中,python将内存中的字符串编译成特殊的代码,然后执行该代码。compile()的作用就是将字符串编译成代码。exec()、eval()都是执行代码,但它们是有区别滴~(看代码中注释)
1 s = "print(123)" 2 #将字符串编译成python代码 3 r = compile(s, ‘<string>‘, ‘exec‘) 4 print(r) 5 #接收代码或字符串,执行python代码(字符串),没有返回值 6 exec(r) 7 输出结果: 8 <code object <module> at 0x00000245086A2780, file "<string>", line 1> 9 123 10 11 s = ‘8 * 8‘ 12 #执行表达式,并且获取结果,有返回值 13 ret = eval(s) 14 print(ret) 15 输出结果: 16 64
dir() help() dir()是快速查看对象提供了哪些功能,help()提供了功能的具体解释,不常用。
print(dir(list)) help(list) 输出结果: [‘__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‘] Help on class list in module builtins: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable‘s items | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __delitem__(self, key, /) | Delete self[key]. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __gt__(self, value, /) | Return self>value. | | __iadd__(self, value, /) | Implement self+=value. | | __imul__(self, value, /) | Implement self*=value. | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self<value. | | __mul__(self, value, /) | Return self*value.n | | __ne__(self, value, /) | Return self!=value. | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __repr__(self, /) | Return repr(self). | | __reversed__(...) | L.__reversed__() -- return a reverse iterator over the list | | __rmul__(self, value, /) | Return self*value. | | __setitem__(self, key, value, /) | Set self[key] to value. | | __sizeof__(...) | L.__sizeof__() -- size of L in memory, in bytes | | append(...) | L.append(object) -> None -- append object to end | | clear(...) | L.clear() -> None -- remove all items from L | | copy(...) | L.copy() -> list -- a shallow copy of L | | count(...) | L.count(value) -> integer -- return number of occurrences of value | | extend(...) | L.extend(iterable) -> None -- extend list by appending elements from the iterable | | index(...) | L.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present. | | insert(...) | L.insert(index, object) -- insert object before index | | pop(...) | L.pop([index]) -> item -- remove and return item at index (default last). | Raises IndexError if list is empty or index is out of range. | | remove(...) | L.remove(value) -> None -- remove first occurrence of value. | Raises ValueError if the value is not present. | | reverse(...) | L.reverse() -- reverse *IN PLACE* | | sort(...) | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None
divmod() 该函数的形式为divmod(被除数,除数),返回的是一个商和余数的元组
r = divmod(98,10) print(r) print(r[0]) print(r[1]) n1, n2 = divmod(99,8) print(n1, n2) 输出结果: (9, 8) 9 8 12 3
enumerate() 遍历序列中的所有元素返回元素的下标和值
seq = [‘alex‘, ‘jack‘, ‘john‘] for i, element in enumerate(seq): print(i, element) 输出结果: 0 alex 1 jack 2 john
isinstance() 用于判断对象是否是某个类的实例 isinstance(对象,类型)
filter() filter(函数,可迭代的对象) ,它会遍历第二个参数里的每一个元素,将这个元素代入到第一个参数里,若函数返回的是Turn,则将这个元素留下,否则抛弃。简而言之。就是它能根据条件过滤数据。
def f1(a): if a > 22: return True lis = [11, 22, 33, 44, 55] res = filter(f1, lis) print(list(res)) 输出结果: [33, 44, 55] #由于f1函数是简单的函数,可以用lambda表达式代替 lis = [11, 22, 33, 44, 55] res = filter(lambda a :a >22, lis) print(list(res))
map() map(函数,可迭代的对象),用于对对象里的每一个元素做操作。它会遍历对象里的每一个元素,将这个元素在函数里执行,将这些结果输出到新的对象中。
lis = [11, 22, 33, 44, 55] res = map(lambda a: a + 100,lis) print(list(res)) 输出结果: [111, 122, 133, 144, 155]
zip() 将多个列表的元素混合起来,返回一个新的列表,里面是元素是元组型。
l1 = [‘linda‘, 22, 33] l2 = [‘happy‘, 22, 33] l3 = [‘everyday‘, 22, 33] new_lis = zip(l1, l2, l3) res = list(new_lis)[0] r = ‘ ‘.join(res) print(r) 输出结果: linda happy everyday
暂时写到这,后续应该还有补充……
标签:
原文地址:http://www.cnblogs.com/mumuzi1990/p/5802769.html