测试代码执行环境为Ubuntu18 + python3.7
range
class range(stop), class range(start, stop[, step]),range 不是一个函数,实际上是一个不可变的序列类型,是一个可迭代对象。常用于for循环中。计数从start开始,到stop结束,不包含stop,step是每次增加的步长。
测试代码:
def test_range():
print("range(5): ", range(5), "; type: ", type(range(5)), "; value: ", list(range(5)))
print("range(0, 5): ", range(0, 5), "; type: ", type(range(0, 5)), "; value: ", list(range(0, 5)))
print("range(2, 5): ", range(2, 5), "; type: ", type(range(2, 5)), "; value: ", list(range(2, 5)))
print("range(2, 10, 1): ", range(2, 10, 1), "; type: ", type(range(2, 10, 1)), "; value: ", list(range(2, 10, 1)))
print("range(2, 10, 3): ", range(2, 10, 3), "; type: ", type(range(2, 10, 3)), "; value: ", list(range(2, 10, 3)))
test_range()
测试结果:
range(5): range(0, 5) ; type: <class 'range'> ; value: [0, 1, 2, 3, 4]
range(0, 5): range(0, 5) ; type: <class 'range'> ; value: [0, 1, 2, 3, 4]
range(2, 5): range(2, 5) ; type: <class 'range'> ; value: [2, 3, 4]
range(2, 10, 1): range(2, 10) ; type: <class 'range'> ; value: [2, 3, 4, 5, 6, 7, 8, 9]
range(2, 10, 3): range(2, 10, 3) ; type: <class 'range'> ; value: [2, 5, 8]
raw_input
raw_input([prompt]), 将所有的输入都转为字符串,在python3中该函数被删除,用input来代替。这里不再介绍。
reduce
reduce(function, iterable[, initializer]),对参数序列中的元素进行累积。将传给reduce的function先对iterable中第一、二个元素进行操作,得到的结果再与第三个元素进行运算,最后得到一个结果。 在python3中该函数移到了functools模块里。
测试代码:
def test_reduce():
def add(x, y):
return x + y
def mul(x, y):
return x*y
l1 = [1, 2, 3, 4, 5, 6]
sum = reduce(add, l1)
print("sum of l1: ", sum)
m = reduce(mul, l1)
print("mul of l1: ", m)
sum = reduce(add, l1, 5)
print("sum of l1 with initializer: ", sum)
m = reduce(mul, l1, 0)
print("mul of l1 with initializer: ", m)
test_reduce()
测试结果:
sum of l1: 21
mul of l1: 720
sum of l1 with initializer: 26
mul of l1 with initializer: 0
reload
reload(module),用于重新载入之前已载入模块。 测试代码:
def test_reload():
import sys
from importlib import reload
print("sys default dlopen flag:", sys.getdlopenflags())
sys.setdlopenflags(9)
reload(sys)
print("sys default dlopen flag:", sys.getdlopenflags())
test_reload()
测试结果:
sys default dlopen flag: 2
sys default dlopen flag: 9
repr
repr(object),返回一个可以表示对象的可打印字符串。该字符串在传递给 eval() 时会创建一个与object具有相同值的对象,或者是一个包含在尖括号中的字符串,其中包含对象类型的名称以及对象的名称和地址等信息。 类可以通过定义 repr() 方法来控制此函数为其实例返回的内容。
测试代码:
def test_repr():
s = "test string"
print("repr of s: ", repr(s))
d = {"adam": 19, "Bob": 20, "Carl": 21}
print("repr of d: ", repr(d))
myclass = MyClass()
print("repr of myclass: ", repr(myclass))
test_repr()
测试结果:
repr of s: 'test string'
repr of d: {'adam': 19, 'Bob': 20, 'Carl': 21}
repr of myclass: <class_define.MyClass object at 0x7efd0bb22150>
reversed
reversed(seq),返回一个反向迭代器。 seq 必须是具有__reversed__() 方法或支持序列协议(__len__()方法和__getitem__()方法,整数参数从 0 开始)的对象。 list有一个类似的内置函数reverse,不过该函数是list特有的,用于列表数据的翻转,会修改列表的内容。
测试代码:
def test_reverse_reversed():
l1 = [1, 2, 3, 4, 5, 6, 7, 8]
print("l1: ", l1)
l1.reverse()
print("l1 after reverse: ", l1)
s1 = "hello world"
s2 = list(reversed(s1))
print("s1: ", s1, "; s2: ", s2)
test_reverse_reversed()
测试结果:
l1: [1, 2, 3, 4, 5, 6, 7, 8]
l1 after reverse: [8, 7, 6, 5, 4, 3, 2, 1]
s1: hello world ; s2: ['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']
round
round(number[, ndigits]),返回四舍五入到小数点后面ndigits位的数,如果ndigits为None的话则返回四舍五入后的整数。 注意,float的round()行为可能会令人奇怪:例如,round(2.675, 2)给出的是2.67,而不是预期的2.68。这不是一个错误:这是因为大多数小数不能精确地表示为浮点。
测试代码:
def test_round():
print("round(0.5): ", round(0.5))
print("round(-0.5): ", round(-0.5))
print("round(2.675, 2): ", round(2.675, 2))
print("round(2.6751, 2): ", round(2.6751, 2))
print("round(80.23456, 2) : ", round(80.23456, 2))
print("round(100.000056, 3) : ", round(100.000056, 3))
print("round(-100.000056, 3) : ", round(-100.000056, 3))
test_round()
测试结果:
round(0.5): 0
round(-0.5): 0
round(2.675, 2): 2.67
round(2.6751, 2): 2.68
round(80.23456, 2) : 80.23
round(100.000056, 3) : 100.0
round(-100.000056, 3) : -100.0
set
class set([iterable]),返回一个新的set对象,它是一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。如果给出iterable则会从中获取元素。
测试代码:
def test_set():
l1 = [1, 2, 3, 4, 5]
s1 = set(l1)
print("s1: ", s1)
l2 = [4, 5, 6, 7, 8]
s2 = set(l2)
print("s2: ", s2)
diff = s1.difference(s2)
print("diff: ", diff)
intersect = s1.intersection(s2)
print("intersection: ", intersect)
un = s1.union(s2)
print("union: ", un)
diff1 = s1 - s2
print("diff1: ", diff1)
intersect1= s1 & s2
print("intersection1: ", intersect1)
un1 = s1 | s2
print("union1: ", un1)
test_set()
测试结果:
s1: {1, 2, 3, 4, 5}
s2: {4, 5, 6, 7, 8}
diff: {1, 2, 3}
intersection: {4, 5}
union: {1, 2, 3, 4, 5, 6, 7, 8}
diff1: {1, 2, 3}
intersection1: {4, 5}
union1: {1, 2, 3, 4, 5, 6, 7, 8}
slice
class slice(stop),class slice(start, stop[, step]),返回一个切片对象,表示由(start,stop,step)指定的一个索引集。start和step默认值为None,也就是第一个形式。这几个值是对象的只读属性。其他的扩展索引语法也会生成切片对象,例如,a[start:stop:step]或a[start:stop, i]。
测试代码:
def test_slice():
my_slice = slice(5)
print("my slice: ", my_slice)
l1 = [1, 2, 3, 4, 5, 6, 7, 8]
l2 = l1[my_slice]
print("l1: ", l1)
print("l2: ", l2)
my_slice1 = slice(2, 8, 2)
l3 = l1[my_slice1]
print("l3: ", l3)
test_slice()
测试结果:
l1: [1, 2, 3, 4, 5, 6, 7, 8]
l2: [1, 2, 3, 4, 5]
l3: [3, 5, 7]
sorted
sorted(iterable, *, key=None, reverse=False),返回一个用iterable中元素排序后生成的新列表。两个可选参数必须使用关键字指定。key是一个拥有一个参数的函数,用来从iterable的元素中提取出排序的键值,默认值为None(直接比较元素)。reverse指定排序规则,reverse = True 降序 , reverse = False 升序(默认)。
测试代码:
def test_sorted():
l1 = [5, 1, 7, 2, 9, 54, 2, 67, 234, 1, 89, 23, 56]
l2 = sorted(l1)
print("l1: ", l1)
print("l2: ", l2)
p1 = Person("Adam", "man", 32, 77)
p2 = Person("Erl", "women", 22, 56)
p3 = Person("Carl", "man", 24, 65)
p4 = Person("Help", "man", 28, 68)
p5 = Person("Jane", "women", 23, 50)
ps = [p1, p2, p3, p4, p5]
ps1 = sorted(ps, key=lambda p: p.age)
for p in ps1:
print("p: ", p)
ps2 = sorted(ps, key=lambda p: p.age, reverse= True)
for p in ps2:
print("p2: ", p)
test_sorted()
测试结果:
l1: [5, 1, 7, 2, 9, 54, 2, 67, 234, 1, 89, 23, 56]
l2: [1, 1, 2, 2, 5, 7, 9, 23, 54, 56, 67, 89, 234]
p: Erl: women, 22, 56
p: Jane: women, 23, 50
p: Carl: man, 24, 65
p: Help: man, 28, 68
p: Adam: man, 32, 77
p2: Adam: man, 32, 77
p2: Help: man, 28, 68
p2: Carl: man, 24, 65
p2: Jane: women, 23, 50
p2: Erl: women, 22, 56
|