python3(九) Section

# list或tuple的部分元素
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
# -----------------传统方法
print([L[0], L[1], L[2]])  # 取前三个值
r = []
n = 3
for i in range(n):
    r.append(L[i])
# 取前n个值
# -------------------切片方法
# 从索引0开始取,直到索引3为止,但不包括索引3
print(L[0:3])  # ['Michael', 'Sarah', 'Tracy']
print(L[1:3])  # ['Sarah', 'Tracy']
print(L[-2:])  # ['Bob', 'Jack']
print(L[-2:-1])  # ['Bob']
# 先创建一个0-99的数列
L = list(range(100))
print(L[:10])  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(L[-10:])  # [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
print(L[10:20])  # [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
print(L[:10:2])  # 前十个数 每两个取一个 [0, 2, 4, 6, 8]
print(L[::5])  # 所有数每五个取一个 [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

# tuple
t = (0, 1, 2, 3, 4, 5)
print(t[:3])  # (0, 1, 2)
print('ABCDEFG'[::2])  # ACEG

 

上一篇:图的算子(属性算子,结构算子,join算子,计算用户粉丝数量,joinVertices和outerJoinVertices的区别)


下一篇:我愿意出2倍工资,挖这个被裁员的程序员