[toc]

进制转换

hex(num), bin(num), oct(num),返回字符串

type()

  1. type(x) # 返回x的数据类型

isinstance()函数

  1. isinstance(x, int) # 判断x是否为int
  2. isinstance([], Iterable) # 判断空列表是否为可迭代对象

enumerate() 函数

  1. enumerate(list_name)

返回一个迭代器,生成(index, list[index])的列表

zip函数

zip接收不定长参数,接收n个列表,返回一个生成器,生成器拼接所有接收的列表形成元组,返回元组的列表。
list(生成器)反复调用生成器直到结束,得到一个列表

>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped1 = list(zip(a,b))     # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
>>> list(zip(a,c))              # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zipped2 = list(zip(*zipped))
[(1, 2, 3), (4, 5, 6)]
>>> zipped1 == list(zip(*zipped2))
True
# 注,这样可以来回转换

注:*nums表示把nums这个list的所有元素作为可变参数传进去。这种写法相当有用,而且很常见。
用zip快速将两个列表变成一个字典

l1 = [1,2,3]
l2 = [a,b,c]
dict(zip(l1,l2))
# 得到字典{1:'a', 2:'b', 3:'c'}

iter函数

把list、str、tuple、set、dict等可迭代对象变成迭代器Iterator
可迭代对象:内部实现了iter()
迭代器:内部实现了iter()和next()

lst = [1, 2, 3]
for i in iter(lst):
    print(i)

next函数

# 首先获得Iterator对象:
it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
    try:
        # 获得下一个值:
        x = next(it)
        print(x)
    except StopIteration:
        # 遇到StopIteration就退出循环
        break

set函数

set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
返回值是一个set对象,可以迭代

map函数

参数1为函数,剩下的参数为该函数的参数
用法一

l=[1,2,3]
x = "".join(list(map(str,l)))
# 对l的每一个元素用str转化为string型,最后x="123"

用法二

提供了两个列表,对相同位置的列表数据进行相加

map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

[3, 7, 11, 15, 19]

reduce函数

reduce把一个函数作用在一个序列[x1, x2, x3, …]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

字符串

编辑操作

去除空格

去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()

内容判断(长度/字母/数字/空格)

是否为空

len(str) == 0

或者

if str:
    print("len(str) > 0")

是否为None

str = None
if str:
    print("str is not None or empty string")
else:
    print("type(str) == <class 'NoneType'>")

空字符串和None转换成bool值都是False

空格 isspace

str = "       "; 
print str.isspace();
str = "This is string example....wow!!!";
print str.isspace();
str = "";
print(str.isspace())
True
False
False

数字/字母 isdigit() isalpha() isalnum()

str_1 = "123"
str_2 = "Abc"
str_3 = "123Abc"
#用isdigit函数判断是否数字
print(str_1.isdigit())
Ture
print(str_2.isdigit())
False
print(str_3.isdigit())
False
#用isalpha判断是否字母
print(str_1.isalpha())    
False
print(str_2.isalpha())
Ture    
print(str_3.isalpha())    
False
#isalnum判断是否数字和字母的组合
print(str_1.isalnum())    
Ture
print(str_2.isalnum())
Ture
print(str_1.isalnum())    
Ture注意:如果字符串中含有除了字母或者数字之外的字符,比如空格,也会返回False

getattr, setattr, hasattr, delattr

hasattr(object, name)

hasattr() 函数用于判断对象是否包含对应的属性。

delattr(object, name)

delattr 函数用于删除属性。

delattr(x, ‘foobar’) 相等于 del x.foobar。

setattr(object, name, value)

setattr() 函数对应函数 getattr(),用于设置属性值,该属性不一定是存在的。

getattr(object, name[, default])

获取对象属性后返回值可直接使用:

>>> class A(object):        
...     def set(self, a, b):
...         x = a        
...         a = b        
...         b = x        
...         print a, b   
... 
>>> a = A()                 
>>> c = getattr(a, 'set')
>>> c(a='1', b='2')
2 1