Python学习笔记3——列表

函数是list(啥啥啥)
方法是 啥啥啥.remove()

1、创建列表
1.1 第一种方式:使用[]

lst = ['hello','world',98]

1.2 第二种方式:使用内置函数list()

lst2 = list(['hello','world',98])

1.3 第三种方式:列表生成式

lst = [i for i in range(1,10)]
print(lst)
lst = [i*i for i in range(1,10)]   #i*i表示列表元素的表达式,通常包含自定义变量
print(lst)
lst = [i*2 for i in range(1,10)]
print(lst)

输出结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[2, 4, 6, 8, 10, 12, 14, 16, 18]

2、索引

"""
正数索引:    0       1     2
          ['hello','world',98]
负数索引:    -3      -2    -1
"""
lst = ['hello','world',98]
print(lst[0],lst[-3])

输出结果:
hello hello

3、列表的特点
#列表可以存储重复数据
#不同类型的数据可以混存
#根据需要动态分配和回收内存

4、获取指定元素的索引
4.1 如果列表中存在N个相同元素,只返回相同元素中第一元素的索引

lst = ['hello','world',98,'hello']
print(lst.index('hello'))

输出结果:
0

4.2 如果要查找的元素在列表中不存在,会报错

#print(lst.index('python'))

4.3 可以在指定的start和stop之间进行查找

#print(lst.index('hello',1,3))  #在索引为1到3(不包括3)之间查找。会报错,因为在此之中没有’hello‘
print(lst.index('hello',1,4))

输出结果:
3

4.4
获取列表中的单个元素:
正向索引:从0到N
逆向索引:从-N到-1

5、获取列表中的多个元素——切片操作
列表名[start:stop:step] 不包含stop

lst = [10,20,30,40,50,60,70,80]
print('————————————step步长为正数的情况————————————')
print(lst[1:6:1])   #不包含6
print(lst[1:6])    #默认步长为1
print(lst[1:6:])
print(lst[1:6:2])
print(lst[:6:2])  #start默认从0开始
print(lst[1::2])  #stop默认到结束

输出结果:
[20, 30, 40, 50, 60]
[20, 30, 40, 50, 60]
[20, 30, 40, 50, 60]
[20, 40, 60]
[10, 30, 50]
[20, 40, 60, 80]

print('————————————step步长为负数的情况————————————')
print('原列表:',lst)
print(lst[::-1])    #反向输出原列表
print(lst[7::-1])   #默认包括0
print(lst[6:0:-2])   #不包括0,包括6

输出结果:
原列表: [10, 20, 30, 40, 50, 60, 70, 80]
[80, 70, 60, 50, 40, 30, 20, 10]
[80, 70, 60, 50, 40, 30, 20, 10]
[70, 50, 30]

6、列表元素的判断及遍历
6.1 判断

lst = [10,20,'python','hello']
print(10 in lst)
print(100 in lst)
print(10 not in lst)
print(100 not in lst)

输出结果:
True
False
False
True

6.2 遍历

for item in lst:
    print(item)

输出结果:
10
20
python
hello

7、列表元素的添加操作
7.1 append()向列表的末尾添加一个元素

lst = [10,20,30]
print("添加元素之前:",lst,id(lst))
lst.append(100)
print("添加元素之后:",lst,id(lst))

输出结果:
添加元素之前: [10, 20, 30] 2888993296840
添加元素之后: [10, 20, 30, 100] 2888993296840

7.2 extend()在列表的末尾至少添加一个元素,可依次添加多个元素

lst2 = ['hello','world']
#lst.append(lst2)   #将lst2作为一个元素添加到后面
lst.extend(lst2)
print(lst)

输出结果:
[10, 20, 30, 100, ‘hello’, ‘world’]

7.3 insert()在列表的任意位置添加一个元素

lst.insert(1,90)   #在位置为1处添加元素90
print(lst)

输出结果:
[10, 90, 20, 30, 100, ‘hello’, ‘world’]

7.4 切片:在列表的任意位置添加至少一个元素(切掉指定位置,用新的列表去替换)

lst3 = [True,False,'hello']
lst[1:] = lst3
print(lst)

输出结果:
[10, True, False, ‘hello’]

8、列表元素的删除操作
8.1 remove()
·一次只删除一个元素
·重复元素只删除第一个
·元素不存在就会报错

lst = [10,20,30,40,50,60,30]
lst.remove(30)
print(lst)
#lst.remove(100) 报错

输出结果:
[10, 20, 40, 50, 60, 30]

8.2 pop()
·删除一个指定索引位置上的元素
·指定索引不存在抛出error
·不指定索引,删除列表的最后一个元素

lst.pop(1)
print(lst)
# lst.pop(5)  报错
lst.pop()
print(lst)

输出结果:
[10, 40, 50, 60, 30]
[10, 40, 50, 60]

8.3 切片:一次至少删除一个元素
至少删除一个元素,产生一个新列表

#至少删除一个元素,产生一个新列表
new_list = lst[1:3]   #不包括3
print('原列表',lst)
print('切片后的列表',new_list)

输出结果:
原列表 [10, 40, 50, 60]
切片后的列表 [40, 50]

如果不想产生新的列表对象,而是想删除列表内的内容,则将想删除的元素用空列表代替

lst[1:3] = []   #用空列表代替
print(lst)

输出结果:
[10, 60]

8.4 clear()清除列表

lst.clear()
print(lst)

输出结果:
[]

8.5 del将列表对象删除

del lst
print(lst)   #这时会报错,说lst没有定义

结果:
NameError: name ‘lst’ is not defined

9、列表元素的修改操作

lst = [10,20,30,40]
lst[2] = 100
print(lst)
#

输出结果:
[10, 20, 100, 40]

切片

lst[1:3] = [300,400,500,600] #将1 2(不包括3)的位置用后面的列表元素代替
print(lst)

输出结果:
[10, 300, 400, 500, 600, 40]

10、列表元素的排序操作
10.1 调用sort()方法,列表中的所有元素默认从小到大的顺序排序,可以指定reverse=True进行降序排序

lst = [12,86,32,7,36]
print('排序前的列表:',lst,id(lst))
lst.sort()   #默认升序
print('排序后的列表:',lst,id(lst))     #不产生新的列表对象
lst.sort(reverse=True)  #降序
print(lst)

输出结果:
排序前的列表: [12, 86, 32, 7, 36] 2134896301064
排序后的列表: [7, 12, 32, 36, 86] 2134896301064
[86, 36, 32, 12, 7]

10.2 调用内置函数sorted(),会产生一个新的列表对象,原列表不发生改变

lst = [12,86,32,7,36]
print('原列表:',lst)
new_list = sorted(lst)
print(lst)
print(new_list)
#指定关键书参数,实现列表元素的降序排序
desc_list = sorted(lst,reverse=True)
print(desc_list)

输出结果:
原列表: [12, 86, 32, 7, 36]
[12, 86, 32, 7, 36]
[7, 12, 32, 36, 86]
[86, 36, 32, 12, 7]

11、列表生成式
见1.3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值