Python 取列表的前几个

原文:https://blog.csdn.net/qq_19741181/article/details/79827963

原文:https://www.runoob.com/python3/python3-list.html

list1 = [1,2,3,4,5,6,7,8]

# 格式: a[start:stop:step]
#   start:开始(包含)
#   stop:结束(不包含该索引)
#   step:步长(间隔数量)

# 实际的意思是:
#   取列表的索引 索引>=2 && 索引<=3 挨着的一串列表
#   (索引>=2 && 索引<=3) 等价于 (索引>=2 && 索引<4)
#   很不巧的是 python选择的是第二种写法,
#       即(索引>=2 && 索引<4)
#       也是网上常说的:前闭后开(等同于数学里面的定义,可以百度一下哈)

res = list1[2:4:1]
print(res)# 输出的结果:[3,4]

res2 = list1[2:4]# 步长为1时 可以省略
print(res2)
原文地址:https://www.cnblogs.com/guxingy/p/12308212.html