Python的列表与操作

1、列表的基础操作

可以将任何东西加入列表中,其中的元素之间可以没有任何关系。

命名:鉴于列表通常包含多个元素,给列表指定一个表示复数的名称(如letters、digits或names)是个不错的主意。

用方括号([])来表示列表,并用逗号来分隔其中的元素。

元素可以通过索引进行访问,索引从0开始,通过将索引指定为-1,可让Python返回最后一个列表元素。索引-2返回倒数第二个列表元素,索引-3返回倒数第三个列表元素,以此类推。但当列表为空时,这种访问这个方式会导致错误。也可以用[m:n]来引用多个从m到n-1,后续会有。当你请求获取列表元素时,Python只返回该元素,而不包括方括号和引号。

bicycles=['trek','cannondale','redline','specialized',100]
print(bicycles)
print(bicycles[0])
print(bicycles[0].title())
print(bicycles[4])
print(bicycles[-1])
print(bicycles[-3])
message="My first bicycle is "+bicycles[0].title()+"."
print(message)

 

1.2、修改 添加 删除元素

修改列表元素,指定要修改的元素的索引,直接赋予新值

也有对应函数,这里涉及的函数无论在前在后,都是直接对列表里面的数据进行修改

motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)

1.2.2 列表添加元素

append()将元素添加到了列表末尾,也适用空列表

insert()可在列表的任何位置添加新元素,在索引位添加空间,其他后后面索引都右移一位

motorcycles=[]
motorcycles.append('honda')
print(motorcycles)
motorcycles=['honda','yamaha','suzuki']
motorcycles.insert(1,'ducati')
print(motorcycles)

 

 

1.2.3 列表删除

del语句删除给定在列表中的位置元素

pop()可删除列表末尾的元素,并得到你删除的元素值。相当于栈 弹出栈顶元素,也可以指定索引位置,删掉特定位置的元素

remove()根据要删除的元素的值来删除元素,remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。

motorcycles=['honda','yamaha','suzuki','ducati']
del motorcycles[0]
print(motorcycles)
del motorcycles[1]
print(motorcycles)

motorcycles=['honda','yamaha','suzuki','ducati']
popped_m=motorcycles.pop()
print(motorcycles,popped_m)
popped_m=motorcycles.pop(1)
print(motorcycles,popped_m)

motorcycles=['honda','yamaha','suzuki','ducati','suzuki']
motorcycles.remove('suzuki')
print(motorcycles)

 

1.3、列表的组织

1.3.1 列表排序

sort()永久性地修改了列表元素的排列顺序,按字母顺序排列的,区分大小写,从a到z,sort(reverse=True)则是从z到a

sorted()按特定顺序 显示 列表元素,同时不影响在列表的原始排列顺序,sorted(reverse=True)同上

reverse()反转列表元素的排列顺序,同永久性修改,但是两次reverse()即可得到原来值

motorcycles=['honda','yamaha','suzuki','ducati']
motorcycles.sort()
print(motorcycles)
motorcycles=['honda','yamaha','suzuki','ducati']
motorcycles.sort(reverse=True)
print(motorcycles)

print("\nsorted(): ")
motorcycles=['honda','yamaha','suzuki','ducati']
print(sorted(motorcycles,reverse=True))
print(motorcycles)

print("\nreverse(): ")
motorcycles=['honda','yamaha','suzuki','ducati']
motorcycles.reverse()
print(motorcycles)
motorcycles.reverse()
print(motorcycles)

1.3.2 计算列表长度

函数len()

motorcycles=['honda','yamaha','suzuki','ducati']
print(len(motorcycles))

结果:4

小结: 发生索引错误却找不到解决办法时,请尝试将列表或其长度打印出来。列表可能与你以 为的截然不同,在程序对其进行了动态处理时尤其如此。通过查看列表或其包含的元素

motorcycles=['honda','yamaha','suzuki','ducati']
for motorcycle in motorcycles:
    print(motorcycle.title()+" is quite good")

数,可帮助你找出这种逻辑错误

2、列表的操作

2.1 遍历列表

motorcycles=['honda','yamaha','suzuki','ducati']
for motorcycle in motorcycles:
    print(motorcycle.title()+" is quite good")

只要没有缩进就算是退出循坏,Python根据缩进来判断代码行与前一个代码行的关系

2.2 创建列表

函数range(m,n): 从你指定的第一个值m开始数,并在到达你指定的第n后停止,因此输出不包含n。range()也可以设定指定步长,函数range(m,n,x)从m开始数,然后不断地加x,直到不超过终值n的最大值(即还是取不到n)

使用range()可以创建列表 三种方式:1、for 2、list() 3、列表解析(推荐)

 

#1、for
squares=[]
for value in range(1,11):
    squares.append(value**2)
print(squares)

#2、list()
squares=list(range(1,10,2))
print(squares) #list没法像其他两者一样对数据进行操作

#3、列表解析
squares=[value**2 for value in range(1,11)]
print(squares)

 

2.3 列表切片

squares[m:n]引用从m到n-1这些数据,squares[:n]从0位开始到n-1,squares[m:]从m位开始到最后,还有这种的squares[-3:]从倒数第三位到最后

squares[:]和squares是不一样的,squares[:]是副本,不是squares本身,要是一个变量=squares,那么squares变则这个变量就会变,变量=squares[:]副本则不会

返回多个元素时,不仅返回元素,而且会包括方括号和引号

motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles[1:3])
print(motorcycles[:2])
print(motorcycles[1:])

#squares[:]和squares区别
print("\n")
my_motorcycles=motorcycles[:]
print(my_motorcycles)
motorcycles[0]='no'
print(my_motorcycles)

print("\n")
motorcycles=['honda','yamaha','suzuki','ducati']#这里前面改了所以改回来一下
my_motorcycles=motorcycles
print(my_motorcycles)
motorcycles[0]='no'
print(my_motorcycles)

2.4 元组

不可变的列表被称为元组。用(),但是引用里面的元素还是用[],不可以修改,但是可以让变量直接重新赋值新的元组

dimensions=(200,100,50)
print(dimensions[0])
print(dimensions[:2])

dimensions=(200,400)
print(dimensions[:])

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值