您的位置:首页 > 编程语言 > Python开发

python 编程 入门到实践 第三章 列表部分 (课后题加原书)

2017-10-12 17:15 267 查看
原书内容加个人理解
#列表,用[]表示列表,并用逗号隔开元素
bicycles=['treck','cannondale','redline','specilized']
print(bicycles)
#访问列表元素,根据其索引位置,用括号括起,即是访问该位置的元素,且索引由1开始,以-1结束
print(bicycles[1])
#采用.title()使其输出更标准
print(bicycles[1].title())
print(bicycles[-1].title())
message=" my bike is "
#通过设置循环打印出所有元素
#list 要输出之前,不可以直接适用range,由于其不是数字,要用Len将其转变为长度
for i in range(len(bicycles)):
print(message+bicycles[i])
#3-2修改,添加和删除元素
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
#修改元素只需要根据其对应位置的索引,将其对应的值修改即可
motorcycles[0]='ducati'
print(motorcycles)
#3.2.2添加元素,采用.append()函数
motorcycles.append('honda')
print(motorcycles)
#在列表中插入元素,采用insert函数,需要指定其索引以及值
motorcycles=['honda','yamaha','suzuki']
motorcycles.insert(0,'ducati')
print(motorcycles)
##删除元素:采用del语句
del motorcycles[0]
print(motorcycles)
del motorcycles[1]
print(motorcycles)
#若在前面已有删除函数,则其删除的值将不会继续出现在列表中,例如第二个print只输出了两个值
#也可以使用pop删除元素
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
#采用一个新变量保留删除的变量
#pop中填的是对应对应元素在变量中的索引,可通过索引改变取改变要删除的值
pop_motorcycles=motorcycles.pop(1)
print(pop_motorcycles)
print(motorcycles)
#若删除列表的某个元素后不再使用,则用del,若要继续使用,则用Pop
#若要删除具体的某个值,可以用remove
motorcycles.remove('honda')
print(motorcycles)
#也可以用新变量替代某个变量名,然后用remove去删除
motorcycles=['honda','yamaha','suzuki']
remove_one='honda'
motorcycles.remove(remove_one)
print(motorcycles)
#3.3组织列表
#sort对列表进行排序(永久),参数reverse为是否采用倒序
cars=['audi','bmw','toyota','subaru']
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)
#sorted为临时排序
print(cars)
print(sorted(cars))
print(cars)
#采用列表的反向排序进行输出
print(cars)
#不可以直接将cars.reverse输入print中,会直接导致输出结果为None
cars.reverse()
print(cars.reverse())

课后题的自己想法

#char 3 作业
#关于列表的循环可用以下两种
#一种采用长度和数字范围进行索引,循环,另一种采用 for name in names
#3.1
names=['jiarui','hexuan','fangcheng']
for i in range(len(names)):
print(names[i].title())
#3-2
message=" hello"
for i in range(len(names)):
print(names[i]+","+message)
#3-3
traffic=['bus','bike','car']
announce="i would like to go to school by "
for type in range(len(traffic)):
print(announce+traffic[type])
#3-4 and 3-5
names=['jiarui','hexuan','fangcheng']
invite='Please come to my dinner , '
for name in names:
print(invite +name)
print('but hexuan may not come')
names[1]='weidong'
for name in names:
print(invite +name)
#3-6
names.insert(0,'jinghao')
names.insert(2,'jiaxu')
names.append('jiahui')
for name in names:
print(invite +name)
# 3-7
for name in names:
print(invite +name)
print('sorry , only two people can come to the dinner')
print(names)
for i in range(len(names)-2):
print(names.pop()+" sorry")
for name in names:
print(name + " you can go to the dinner ")
# del names
# print(names)
#3-8
places=['hangzhou','suzhou','shanghai','beijing','chengdu']
print(places)
print(sorted(places))
print(sorted(places,reverse=True))
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse=True)
print(places)
#3-9
print(len(names))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: