slice()函数创建一个slice对象,该对象可用于对字符串,列表,元组等进行切片。
slice对象用于切片给定的序列(字符串,字节,元组,列表或范围)或任何支持序列协议的对象(实现__getitem__()和__len__()方法)。
slice 语法:
class slice(stop)
class slice(start, stop[, step])
my_slice = slice(5) my_slice2 = slice(2, 10, 2) arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(arr[my_slice]) # [0, 1, 2, 3, 4] print(arr[my_slice2]) # [2, 4, 6, 8] print(my_slice.start,my_slice.stop,my_slice.step) print(my_slice2.start,my_slice2.stop,my_slice2.step) ###### 0123456789012345678901234567890123456789012345678901234567890' record = '....................100 .......513.25 ..........' # cost = int(record[20:23]) * float(record[31:37]) SHARES = slice(20, 23) PRICE = slice(31, 37) cost = int(record[SHARES]) * float(record[PRICE]) print(cost)
标签:__,slice,函数,slice2,创建,record,print,my From: https://www.cnblogs.com/sangern/p/17448975.html