第三章 列表【Python从入门到实践】

 最后有彩蛋:

#列表
bicycle=['trek','cannondale','readline']
print(bicycle)

#访问列表
print(bicycle[0])
print(bicycle[-1])

#使用列表中的各个值
message="my first bicycle was a "+bicycle[0].title()+"."
print(message)

#修改列表元素:利用索引直接修改
motorcycles=['honda','yamaha','zuzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)

#添加列表元素
motorcycles.append('ducati')    #在末尾添加元素
print(motorcycles)
motorcycles.insert(0,'ducati')   #根据索引,在指定位置添加元素
print(motorcycles)

#删除列表元素:直接删除了,删除就删除了,这个值不可以给别人
del motorcycles[0]
print(motorcycles)

#pop()函数:类似于剪切,这个值可以给别人
motorcycles.pop()                     #弹出列表中最后位置的元素
print(motorcycles)

first_moto=motorcycles.pop(0)        #弹出列表中任意位置的元素:变量.pop(0)
print("The first motorcycle I won was a "+first_moto.title()+"!")

poped_motorcycles=motorcycles.pop()    #有时还需要运用到pop()出来的元素
print(motorcycles)
print(poped_motorcycles)

#remove(~):不知道索引位置,根据值删除元素,remove出去的不可以赋值继续运用
motorcycles=['honda','yamaha','zuzuki','ducati']
print(motorcycles)
motorcycles.remove('yamaha')
print(motorcycles)

#变量.sort()/变量.sort(reverse=False):对列表首元素字母进行永久性<正向>排序:
motorcycles=['honda','yamaha','zuzuki','ducati']
motorcycles.sort()   #等价于: motorcycles.sort(reverse=False)
print(motorcycles)

#变量.sort(reverse=False):对列表首元素字母永久性<反向>排序:
motorcycles=['honda','yamaha','zuzuki','ducati']
motorcycles.sort(reverse=True)
print(motorcycles)

#sorted(car):临时性排序
cars=['bwm','audi','toyoto','subarn']
print('here is the original list:')
print(cars)
print('here is the sorted list:')
print(sorted(cars))
print(cars)    #['bwm', 'audi', 'toyoto', 'subarn']

#变量.reverse():倒着打印
cars.reverse()
print(cars)

#列表长度:len(cars)
print(len(cars))

#3-1 姓名列表,访问列表,打印出来。
names=['a','b','c','d']
for name in names:
    print(name)

#3-2 姓名列表,打印人名+问候语。
names=['a','b','c','d']
for name in names:
    print(name+', how are you?')

#3-3 多种通勤方式列表,打印一些通勤方式宣言
transports=['car','biycecle','walk','train','moto']
# for way in ways:
#     print("I would like to won"+way+"in london")
print("I would like to won" + transports[0] + "in london")
print("I would like to won" + transports[1] + "in my hometown")

#3-4 嘉宾名单列表,邀请共进晚餐
guests=['LingLing','Pangyan','Tom',]
for guest in guests:
   print("welcome to the wedding "+guest)


#3-5 修改嘉宾列表
absent_guests=guests[2]         #指出无法出席的嘉宾
guests.remove(absent_guests)
print(absent_guests+" cannot come")
guests.append('DanDan')         #添加新嘉宾
for guest in guests:    #邀请每位嘉宾
   print("welcome to the wedding "+guest)

#3-6 分别位于列表开头、中间、结尾
guests.insert(0,'Mark')
guests.insert(4,'Tank')
guests.append('Freya')
for guest in guests:    #邀请每位嘉宾
   print("welcome to the wedding "+guest)

#3-7 缩减名单,利用pop()不断删除嘉宾,只剩下2个嘉宾---->名单变空
for guest in guests[2:6]:
    a = guests.pop()
    print("The wedding is canceled,franchement desole,"+a)

for guest in guests[0:2]:
    print("welcome to the wedding " + guest)

print(guests)
print(guests[0])
print(guests[1])
del guests[0]    #删除余下2位
del guests[0]
print(guests)

#3-8 5个渴望旅游的地方
travels=['nanjing','dali','hangzhou','xi an','dalian',]
#打印原始列表
print("the original list:")
print(travels)

#sorted()临时性:按字母排序打印
print("\nsorted list:")
print(sorted(travels))
print("再次确认sorted()不改变原来列表的顺序:")
print(travels)      #再次确认不改变原来列表的顺序

#reverse():反序打印,改变原来列表的顺序
travels.reverse()
print("\nrevese反转后的效果")
print(travels)     #再次确认改变原来列表的顺序

#用reverse()再次反转,恢复到原始顺序
travels.reverse()
print("\nrevese再次反转后的效果")
print(travels)

#sort(reverse=True)反序:永久性反序
print("\nsort list:")
travels.sort(reverse=True)
print(travels)

#3-9 邀请了几个嘉宾
guests=['LingLing','Pangyan','Tom',]
print(len(guests))

#3-10 自建列表,尝试所有函数
#1.访问
#2.修改
#3.添加
#4.删除

#3-11 有意引发错误

['trek', 'cannondale', 'readline']
trek
readline
my first bicycle was a Trek.
['honda', 'yamaha', 'zuzuki']
['ducati', 'yamaha', 'zuzuki']
['ducati', 'yamaha', 'zuzuki', 'ducati']
['ducati', 'ducati', 'yamaha', 'zuzuki', 'ducati']
['ducati', 'yamaha', 'zuzuki', 'ducati']
['ducati', 'yamaha', 'zuzuki']
The first motorcycle I won was a Ducati!
['yamaha']
zuzuki
['honda', 'yamaha', 'zuzuki', 'ducati']
['honda', 'zuzuki', 'ducati']
['ducati', 'honda', 'yamaha', 'zuzuki']
['zuzuki', 'yamaha', 'honda', 'ducati']
here is the original list:
['bwm', 'audi', 'toyoto', 'subarn']
here is the sorted list:
['audi', 'bwm', 'subarn', 'toyoto']
['bwm', 'audi', 'toyoto', 'subarn']
['subarn', 'toyoto', 'audi', 'bwm']
4
a
b
c
d
a, how are you?
b, how are you?
c, how are you?
d, how are you?
I would like to woncarin london
I would like to wonbiyceclein my hometown
welcome to the wedding LingLing
welcome to the wedding Pangyan
welcome to the wedding Tom
Tom cannot come
welcome to the wedding LingLing
welcome to the wedding Pangyan
welcome to the wedding DanDan
welcome to the wedding Mark
welcome to the wedding LingLing
welcome to the wedding Pangyan
welcome to the wedding DanDan
welcome to the wedding Tank
welcome to the wedding Freya
The wedding is canceled,franchement desole,Freya
The wedding is canceled,franchement desole,Tank
The wedding is canceled,franchement desole,DanDan
The wedding is canceled,franchement desole,Pangyan
welcome to the wedding Mark
welcome to the wedding LingLing
['Mark', 'LingLing']
Mark
LingLing
[]
the original list:
['nanjing', 'dali', 'hangzhou', 'xi an', 'dalian']

sorted list:
['dali', 'dalian', 'hangzhou', 'nanjing', 'xi an']
再次确认sorted()不改变原来列表的顺序:
['nanjing', 'dali', 'hangzhou', 'xi an', 'dalian']

revese反转后的效果
['dalian', 'xi an', 'hangzhou', 'dali', 'nanjing']

revese再次反转后的效果
['nanjing', 'dali', 'hangzhou', 'xi an', 'dalian']

sort list:
['xi an', 'nanjing', 'hangzhou', 'dalian', 'dali']
3

我思故我在:

原因:

删除前:

guests[0]guests[1]
MarkLingLing

del[0]之后:‘LingLing’为guests[0],guests列表中不存在guests[1]

guests[0]
LingLing

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值