目录
enumerate(sequence, [start=0])
str.split(str="", num=string.count(str))
str.strip(self, chars=None, /)
str.lstrip(self, chars=None, /)
str.rstrip(self, chars=None, /)
【教程】
廖雪峰:https://www.liaoxuefeng.com/wiki/1016959663602400
菜鸟教程:https://www.runoob.com/python3/python3-tutorial.html
W3Cschool学院:https://www.w3cschool.cn/python3/python3-tutorial.html
1 库函数
1.1python内置函数
所有内置函数:https://www.runoob.com/python3/python3-built-in-functions.html
enumerate(sequence, [start=0])
- sequence -- 一个序列、迭代器或其他支持迭代对象。
- start -- 下标起始位置。
功能:enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。其实就是添加了一个索引值。
Python3 enumerate() 函数:https://www.runoob.com/python3/python3-func-enumerate.html
# eg:此处仅读取MNIST的测试集
>>> test_loader = torch.utils.data.DataLoader(datasets.MNIST(dataset_path, train=False, \
download=False,transform=transforms.ToTensor()),batch_size=batch_size, shuffle=True)
>>> for x, y in test_loader:
... print(x.size()) # x 表示数据 (batch_size, C, W, H)
... print(y.size()) # y 表示标签 (batch_size)
# enumerate本质就是添加了一个索引index
>>> for index, img in enumerate(test_loader):
... print(index)
... print(img[1].size())
... print(img[1].size())
... break
0
torch.Size([64, 1, 28, 28]) # 此处batch_size = 64
torch.Size([64])
super()
功能:调用父类(超类)的一个方法
Python super() 函数:https://www.runoob.com/python/python-func-super.html
super() 函数是用于调用父类(超类)的一个方法,而 Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx
class A:
def add(self, x):
y = x+1
print(y)
class B(A):
def add(self, x):
super().add(x)
# super(B, self).add(x) # Python 2.x
b = B()
b.add(2) # 3
关于 super() 的初始化功能:Python—子类构造函数调用super().__init__()
format()
功能:格式化字符
Python format 格式化函数:https://www.runoob.com/python/att-string-format.html
print("{} {}".format('jack', 'tom')) # jack tom
print("{1} {0} {1}".format('jack', 'tom')) # tom jack tom
print("姓名:{name}, 年龄:{age}".format(name='jack', age=25))
person = {'name':'jack', 'age':25}
print("姓名:{name}, 年龄:{age}".format(**person))
person1 = ['jack', 25]
print("姓名1:{0[0]}, 年龄1:{0[1]}".format(person1)) # 0 不能省略
person2 = ['tom', 24]
print("姓名1:{0[0]}, 年龄1:{0[1]}\n姓名2:{1[0]}, 年龄2:{1[1]}".format(person1, person2))
person3 = [person1, person2]
print("姓名1:{0[0][0]}, 年龄1:{0[0][1]}\n姓名2:{0[1][0]}, 年龄2:{0[1][1]}".format(person3))
f-Strings
功能:格式化字符串(推荐使用这个)
name = "Eric"
age = 74
>>> print(f"Hello, {name}. You are {age}.") # f大写也可以
'Hello, Eric. You are 74.'
>>> print(f'{2*23}') # 运行时渲染,支持表达式
'46'
>>> name = 'Eric'
>>> f"{name.lower()} is funny." # 当然也支持函数
'eric is funny.'
print(..., end='\r')
添加这一句可以让控制台定行输出
map( func, *