学习python -- 第006天 列表(一)

列表(一)

列表对象的删除与创建及其特点

 1 #
 2 # @author:浊浪
 3 # @time: 2021/3/9 22:26
 4 
 5 a = 1  #变量存储的是一个对象的引用
 6 lis = ['hello', 'nm', 996]
 7 print(id(lis))
 8 print(type(lis))
 9 print(lis)
10 
11 
12 '''创建列表的两种方式'''
13 # 第一种 直接
14 lst1 = ['hello', 'wwww', 996]
15 print(lst1[1], lst1[-2])
16 
17 # 第二种
18 lst2 = list(['hello', 'wwww', 996])

列表的查询操作

 1 #
 2 # @author:浊浪
 3 # @time: 2021/3/9 22:43
 4 
 5 lst = ['hello', 'nm', 996, 'hello']
 6 print(lst.index('hello'))  #如果类表中有相同元素则返回第一个元素的索引
 7 
 8 # print(lst.index('pyhton'))  #ValueError: 'pyhton' is not in list
 9 # print(lst.index('hello', 1, 3))  #ValueError: 'hello' is not in list 查询范围是[1,3)
10 
11 print(lst.index('hello', 1, 4))

 1 #
 2 # @author:浊浪
 3 # @time: 2021/3/9 22:52
 4 lst = [10, 20, 30, 40, 50, 60, 70]
 5 print('原列表:', id(lst))
 6 
 7 # start = 1, stop = 6, step = 1
 8 lst2 = lst[1:6:1]
 9 print('切片后结果:', id(lst2))
10 
11 print(lst)
12 print(lst2)
13 
14 print(lst[1:6])  #默认步长为1 有无冒号:无所谓
15 print(lst[1:6:])
16 
17 print(lst[:6:2])  #默认的start为0
18 
19 print((lst[::3]))  #默认stop为最后
20 print('----------------step为负数的情况----------------------')
21 
22 print(lst[::-1])  #[70, 60, 50, 40, 30, 20, 10]
23 
24 print(lst[6::-1])  #[70, 60, 50, 40, 30, 20, 10]
25 
26 print(lst[6:0:-2])  #[70, 50, 30]

认清现实,放弃幻想。 细节决定成败,心态放好,认真学习与工作。
原文地址:https://www.cnblogs.com/jyf2018/p/14508844.html