Python自学记录-第3周

Python自学记录目录

Python自学记录-第1周
Python自学记录-第2周



前言

接着上周的python进度学习,本周学习——列表


一、什么是列表?

  1. 列表是有序的序列,但其中的每个元素类型可以各不相同
  2. 可根据索引找到数据,可切片,可以增删改查,排序
  3. 创建一个列表只要向"[]"里面放数据就可以了

二、实例

1.索引

1.1 根据索引号,找到列表中的值,相同的值以索引号低的优先

'''创建列表第一种方式,使用[]'''
lst=['hello','world',1998]
print(lst,id(lst),type(lst))
print(lst[0],lst[-2])   # 负数代表倒数的序号是多少

'''创建列表的第二种方式,使用内置的函数list()'''
lst2=list(['hello','world',1998])
print(lst2,id(lst2),type(lst2))

运行结果

['hello', 'world', 1998] 2001080548032 <class 'list'>
hello world
['hello', 'world', 1998] 2001080795456 <class 'list'>

1.2 根据索引号,找到列表中的值,相同的值以索引号低的优先

lst=['hello','world',98,'hello']
print(lst.index('hello'))   # 列表中有相同元素则只返回相同元素的第一个元素的索引 这里返回0

# print(lst.index('python'))  # ValueError: 'python' is not in list
# print(lst.index('hello', 1, 3))     # ValueError: 'hello' is not in list

print(lst.index('hello', 1, 4))     # 在索引为1的和为4的之间找,左边闭区间,右边开区间 返回3
print(lst.index('hello', 0, 2))     # 返回0

运行结果

0
3
0

1.3 索引可正可负,负值则按照倒序排列


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

# 获取索引为-3的元素
print(lst[-3])

# 获取索引为10的元素
# print(lst[10])  # IndexError: list index out of range

运行结果

hello

2.列表表达式

创建列表非常方便,后面应该会很常用


lst=[i*i for i in range(1,11)]
print(lst)

'''列表中元素为2 4 6 8 10'''
lst2=[i*2 for i in range(1,6)]
print(lst2)


运行结果

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[2, 4, 6, 8, 10]

3.增删改查

增:加一个数据,类似链表,可插入随意位置;删,删除数据,也可删除整个列表(删库跑路?);改:修改数据;查:根据索引快速查数据

3.1 增加元素,可在末尾加也可直接插入


# 向末尾添加一个元素
lst=[10,20,30]
print('添加前',lst,id(lst))

lst.append(100)
print('添加后',lst,id(lst))

lst2=['hello','Mr.Shu']
lst.append(lst2)
print(lst)

#任意位置添加
lst.insert(1,99)
print(lst)

lst3=[True,False,'Hello']
# 任意位置添加N个元素
lst[1:]=lst3
print(lst)


运行结果

添加前 [10, 20, 30] 1464177392320
添加后 [10, 20, 30, 100] 1464177392320
[10, 20, 30, 100, ['hello', 'Mr.Shu']]
[10, 99, 20, 30, 100, ['hello', 'Mr.Shu']]
[10, True, False, 'Hello']

3.2 删除元素,索引删,直接删,切片删,删除表等等

lst=list(range(10,70,10))
lst.append(30)
print(lst)

# remove和append类似,从首个开始处理
lst.remove(30)
print(lst)

# 没有的元素删除就报错
# lst.remove(100)
print(lst)

print('--------索引删除--------')
#根据索引删除元素
lst.pop(1)
print(lst)

# 若不指定参数,则删除最后的参数
lst.pop()
print(lst)

# 切片操作
print('----------切片操作----------')
new_list=lst[1:3]
print('原列表',lst)
print('切片后的列表',new_list)

'''不产生新的列表,也删除列表中的内容'''
lst[1:3]=[]
print(lst)

'''清除列表中所有元素'''
lst.clear()
print(lst)

'''将列表对象删除'''
del lst
# print(lst) # NameError: name 'lst' is not defined



运行结果

[10, 20, 30, 40, 50, 60, 70, 80, 90]
原列表ID 1558858986816
[20, 30, 40, 50, 60]
[20, 30, 40, 50, 60]
切的片段ID 1558858987264
[20, 30, 40, 50, 60]
[20, 30, 40, 50, 60]
[20, 40, 60]
[10, 30, 50]
[20, 40, 60, 80]
--------步长为负数--------
[90, 80, 70, 60, 50, 40, 30, 20, 10]
[80, 70, 60, 50, 40, 30, 20, 10]
[70, 50, 30]

3.3 修改元素,直接修改

lst=list(range(10,50,10))

#一次修改一个值
lst[2]=100
print(lst)

lst[1:3]=[300,400,500,600]
print(lst)

运行结果

[10, 20, 100, 40]
[10, 300, 400, 500, 600, 40]

4.排序

简简单单实现排序功能,可升序可降序,可以说非常nice了!

lst=[20,40,10,98,54]
print('排序前的列表',lst,id(lst))

#sort升序
lst.sort()
print('排序后的列表',lst,id(lst))

# 关键字指定,元素降序
lst.sort(reverse=True)
print(lst)

lst.sort(reverse=False)
print(lst)

print('----------使用sorted()排序,产生新的列表---------')
# lst=[i for i in range()]
lst=[20,40,10,98,54]

lst2=sorted(lst)
print('原列表',lst)
print('新列表',lst2)

运行结果

排序前的列表 [20, 40, 10, 98, 54] 3016378743488
排序后的列表 [10, 20, 40, 54, 98] 3016378743488
[98, 54, 40, 20, 10]
[10, 20, 40, 54, 98]
----------使用sorted()排序,产生新的列表---------
原列表 [20, 40, 10, 98, 54]
新列表 [10, 20, 40, 54, 98]

Process finished with exit code 0

总结

总的来说,列表相较于C的数组来说,可以轻松增删改查和排序,还能支持各种数据类型,非常方便。
下周继续学习——字典。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值