1、基本用法
>>> test1 = ["aa","bb","cc","dd","ee","ff"]
>>> type(test1)
<class 'list'>
>>> len(test1)
6
>>> test1[0:3] ## 不包含索引为3的元素
['aa', 'bb', 'cc']
>>> test1[2:4] ## 不包含索引为4的元素
['cc', 'dd']>>> test1[:4]
['aa', 'bb', 'cc', 'dd']
>>> test1[2:]
['cc', 'dd', 'ee', 'ff']
>>> test1[:] ## 完整列表
['aa', 'bb', 'cc', 'dd', 'ee', 'ff']
>>> test1[-3:] ## 取后三个元素
['dd', 'ee', 'ff']
2、指定步长
>>> test1 = ["aa","bb","cc","dd","ee","ff","gg","hh","ii","jj"] >>> type(test1) <class 'list'> >>> len(test1) 10 >>> test1[0:9:1] ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii'] >>> test1[0:10:1] ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj'] >>> test1[0:10:2] ['aa', 'cc', 'ee', 'gg', 'ii'] >>> test1[::2] ['aa', 'cc', 'ee', 'gg', 'ii'] >>> test1[0:10:3] ['aa', 'dd', 'gg', 'jj'] >>> test1[::3] ['aa', 'dd', 'gg', 'jj'] >>> test1[::4] ['aa', 'ee', 'ii'] >>> test1[0:6] ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'] >>> test1[0:6:2] ['aa', 'cc', 'ee'] >>> test1[0:6:3] ['aa', 'dd']
3、列表反转
>>> test1[::1] ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj'] >>> test1[::-1] ['jj', 'ii', 'hh', 'gg', 'ff', 'ee', 'dd', 'cc', 'bb', 'aa']