[python笔记]3.列表

一.访问列表元素

1)基本访问方式

bicycles=['trek','cannondale','redline','specialized']
print(bicycles)#输出列表中的所有元素
print(bicycles[0])#按索引访问
print(bicycles[0].title())#列表中元素调用方法

输出

['trek', 'cannondale', 'redline', 'specialized']
trek
Trek

(其中按索引访问第一个列表元素的索引为0,可以访问-1,-2…表示列表的倒数第一第二个元素)
(当列表为空时 -1会引发错误)

print(bicycles[-1])
print(bicycles[-2])

输出

specialized
redline

2)使用列表中的值访问

message="My first bicycle was a "+bicycles[0].title()+"."
print(message)

输出

print(message)

二.列表修改,添加,删除

1)修改列表元素

直接用索引指定新元素的值

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

输出

['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']

2)列表中添加元素

1.append()方法:把元素添加到列表末尾

motorcycles.append('ducati')
print(motorcycles)

输出

['ducati', 'yamaha', 'suzuki', 'ducati']

可以使用append()方法动态创建列表

motorcycles=[]
print(motorcycles)
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)

输出

[]
['honda', 'yamaha', 'suzuki']

2.insert()方法:在列表中插入元素(任何位置)需要指定新元素与索引值

motorcycles.insert(1,'ducati')
print(motorcycles)

输出

['honda', 'ducati', 'yamaha', 'suzuki']

3)从列表中删除元素

1.知道要删除元素在列表中的位置 del语句

print(motorcycles)
del motorcycles[0]
print(motorcycles)

输出

['honda', 'ducati', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']

2.pop()方法删除元素:
列表就像一个栈,删除列表末尾的元素相当与弹出栈顶元素
弹出的元素存储在可以一个新的变量中。

motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcyles=motorcycles.pop()
print(motorcycles)
print(popped_motorcyles)

输出

['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki

3.**pop()**方法删除任何位置元素,在括号中指出被删除元素的索引即可

motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
first_owned=motorcycles.pop(0)
print(motorcycles)
print("The first motorcyle I owned was a "+first_owned.title()+".")

输出

['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
The first motorcyle I owned was a Honda.

pop()与delete的区别 从列表中删除一个元素,并且不再使用,就是delete语句,如果还要使用,就是pop()方法
删除可以直接用pop,如果需要用这个元素定义一个变量接着就好 类似栈

4.remove()方法:根据元素值删除元素

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

输出

['yamaha', 'suzuki'] 	

如果列表中有重复的元素,会根据顺序删除第一个值为给定的元素

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

输出

['yamaha', 'honda', 'suzuki']
['yamaha', 'suzuki']

remocve()的参数也可以是定义的变量,变量中可以保存要删除变量的值,这样就可以继续使用这个变量

motorcycles=['ducati','yamaha','honda','suzuki']
too_expensive='ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("\nA "+too_expensive.title()+" is too expensive for me")

输出

['ducati', 'yamaha', 'honda', 'suzuki']
['yamaha', 'honda', 'suzuki']

A Ducati is too expensive for me

三.组织列表

1)对列表进行排序

1.sort()方法:对列表进行永久性排序
sort()按字典顺序排列

cars=['bmw','audi','toyota','subaru']
print(cars)
cars.sort()
print(cars)

输出

['bmw', 'audi', 'toyota', 'subaru']
['audi', 'bmw', 'subaru', 'toyota']

反向排序(倒序),令参数reverse=True

cars=['bmw','audi','toyota','subaru']
print(cars)
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)

输出

['bmw', 'audi', 'toyota', 'subaru']
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']

2.sorted()函数 临时排序
能够按特定顺序显示列表元素,不影响原始序列,也可以传递reverse=True参数降序排列

cars=['bmw','audi','toyota','subaru']
print(sorted(cars))
print(cars)
print(sorted(cars,reverse=True))

输出

['audi', 'bmw', 'subaru', 'toyota']
['bmw', 'audi', 'toyota', 'subaru']
['toyota', 'subaru', 'bmw', 'audi']

2)倒着打印列表元素

reverse()方法:反转列表元素的排列顺序(永久性)

cars=['bmw','audi','toyota','subaru']
print(cars)
cars.reverse()
print(cars)

输出

['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']

3)列表长度

函数len(),获取列表长度,即包含元素的个数,或是列表中的__len__()方法

cars=['bmw','audi','toyota','subaru']
print(len(cars))
print(cars.__len__())

输出

4
4

四.遍历列表

1)for循环

magicians=['alice','david','carnolia']
for magican in magicians:
    print(magican) 

输出

alice
david
carnolia

python循环没有括号括起来,缩进部分就是循环的部分,注意缩进的格式

magicians=['alice','david','carnolia']
for magican in magicians:#注意冒号
	##循环体
    print(magican.title()+", that was a great trick!")
    print("I can't wait to see your next trick, "+magican.title()+".\n")
#出循环
print("Thank you,everyone. That was a great magic show")

输出

I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carnolia, that was a great trick!
I can't wait to see your next trick, Carnolia.

Thank you,everyone. That was a great magic show

五.数字列表

1)range()函数

函数原型:range(start, end, scan):start未开始,默认为0,end为结束(打印到end的前一个,不会打印end),scan为跳跃步数,默认为1

可以理解为c语言的

for(int i=start;i<end;i+=scan){}

eg:

#默认从0开始
for value in range(3):
    print(value)

输出

0
1
2
for value in range(1,5):
    print (value)

输出

1
2
3
4
#输出偶数
for value in range(2,11,2):
    print(value)

输出

2
4
6
8
10

range(a,b),从a开始,在到达b之后停止,所以不会打印b,如果要打印1-5 就需要range(1,6)

2)创建数字列表

使list()函数将range()的结果转换为列表,即将range()作为list()的参数

numbers=list(range(1,6))
print(numbers)

输出

[1, 2, 3, 4, 5]
numbers=list(range(2,11,2))
print(numbers)

输出

[2, 4, 6, 8, 10]
for value in range(1,11):
    square = value**2
    squares.append(square)
print(squares)

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

输出

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

3)对数字列表执行简单统计计算

处理数字列表的python函数 max() min() sum()

digits=[1,2,3,4,5,6,7,8,9,0]
print(min(digits))
print(max(digits))
print(sum(digits))

输出

0
9
45

六.使用部分列表

1)切片

players=['charles','martina','michael','florence','eli']
print(players[0:3])
print(players[1:4])
#print(players[a:b])如果a没有指定,从头(0号元素)开始,如果b没有指定, 则一直到末尾
print(players[:4])
print(players[2:])
#使用负数表示列表中倒数第几个元素,如输出最后三名队员
print(players[-3:])

输出

['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']

2)遍历切片

可以在for循环中直接遍历切片的列表

for player in players[:3]:
    print(player.title())

输出

Charles
Martina
Michael

3)复制列表

my_foods=['pizza','falafel','carrot cake']
firend_foods=my_foods[:]
print(my_foods)
print(firend_foods)
my_foods.append('cannoli')
firend_foods.append('ice cream')
print(my_foods)
print(firend_foods)
['pizza', 'falafel', 'carrot cake']
['pizza', 'falafel', 'carrot cake']
['pizza', 'falafel', 'carrot cake', 'cannoli']
['pizza', 'falafel', 'carrot cake', 'ice cream']

注:上面的复制firend_foods=my_foods[:] 其中friends_food与my_foods是作为两个独立列表,但不能直接firend_foods=my_foods,这种情况是将新变量friend_food关联到my_food中的列表,因此这两个变量指向同一个列表。

my_foods=['pizza','falafel','carrot cake']
firend_foods=my_foods
print(my_foods)
print(firend_foods)
my_foods.append('cannoli')
firend_foods.append('ice cream')
print(my_foods)
print(firend_foods)

输出

['pizza', 'falafel', 'carrot cake']
['pizza', 'falafel', 'carrot cake']
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

如果需要复制列表,就按上面第一种方式进行复制

七.元组

创建一系列不可修改的元素,即不可变的列表称为元组

1)定义

元组使用圆括号,可使用索引来访问,同列表

dimensions=(200,50)
print(dimensions[0])
print(dimensions[1])

输出

200
50

如果要修改

dimensions[0]=250

输出

TypeError: 'tuple' object does not support item assignment

2)遍历元组

for dimension in dimensions:
    print(dimensions)

输出

200
50

3)修改元组变量

虽然不可以修改元组中的变量,但可以直接给整个元组赋值

dimensions=(200,50)
print('Original dimensions:')
for dimension in dimensions:
    print(dimension)
dimensions=(400,100)
print('\nModified dimensions')
for dimension in dimensions:
    print(dimension)
#重新赋值为(400,100),但不能分别修改元组中的变量

输出

Original dimensions:
200
50

Modified dimensions
400
100
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值