python入门脚本练习之列表list
作者:互联网
1、 将一些朋友的姓名存储在一个列表中, 并将其命名为names 。 依次访问该列表中的每个元素, 从而将每个朋友的姓名都打印出来。
names=['liya','liuyang','liyaping','lijinduo']
for i in names:
print(i,' Would you like to have supper with me ?')
2、你刚得知有位朋友无法赴约,因此需要邀请另外有一个朋友
(1)以1为基础,在程序末尾添加一条print语句,指出是哪位朋友无法赴约
names=['liya','liuyang','liyaping','lijinduo']
for i in names:
print(i,' Would you like to have supper with me ?')
print(names[0],"'s Busy")
(2)修改嘉宾名单,无法赴约的朋友替换为新朋友,并再次向各位朋友邀约
names=['liya','liuyang','liyaping','lijinduo']
for i in names:
print(i,' Would you like to have supper with me ?')
print(names[0],"'s Busy")
names[0]='zhangsan'##按照索引修改
for i in names:
print(i, ' Would you like to have supper with me ?')
3、你刚找到更大的一个餐桌,可容纳更多的人
(1)在2的任意编写的为基础,在程序末尾添加,指出你找到了一个更大的餐桌。
names=['liya','liuyang','liyaping','lijinduo']
names[0]='zhangsan'##按照索引修改
for i in names:
print(i, ' Would you like to have supper with me ?')
print('I found a bigger table')
(2)使用insert()将一位新朋友添加之名单开头
names=['liya','liuyang','liyaping','lijinduo']
names[0]='zhangsan'##按照索引修改
for i in names:
print(i, ' Would you like to have supper with me ?')
print('I found a bigger table')
names.insert(0,'lisi')
print(names)
(3)使用insert()将一位朋友添加至名单中间
names=['liya','liuyang','liyaping','lijinduo']
names[0]='zhangsan'##按照索引修改
for i in names:
print(i, ' Would you like to have supper with me ?')
print('I found a bigger table')
names.insert(0,'lisi')
print(names)
names.insert(2,'zhaoliu')
print(names)
(4)使用append()添加一位朋友至名单末尾,并向名单中每一位朋友发出邀请
names=['liya','liuyang','liyaping','lijinduo']
names[0]='zhangsan'##按照索引修改
for i in names:
print(i, ' Would you like to have supper with me ?')
print('I found a bigger table')
names.insert(0,'lisi')
print(names)
names.insert(2,'zhaoliu')##insert可以任意位置添加
print(names)
names.append('sunqi')##append从列表末尾添加
print(names)
for i in names:
print(i, ' Would you like to have supper with me ?')
4、想出5个你最渴望去的旅游的地方
(1)将这些地方存储在列表中,并确保不是按照字母顺序排序的,打印出原始列表
tour=['大理','土耳其','新疆','成都','西安']
print(tour)
(2)使用sorted按字母顺序打印这个列表,不要修改它
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
(3)再次打印该列表,核实该列表未改变
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
print(tour)
(4)使用sorted()按与字母相反的顺序打印这个列表,同时不要修改他
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
print(tour)
print(sorted(tour,reverse=True))
(5)再次打印该列表,核实并未改变位置
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
print(tour)
print(sorted(tour,reverse=True))
print(tour)
(6)使用reverse()修改列表元素的排列顺序,打印该列表,核实排列顺序是否变了
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
print(tour)
print(sorted(tour,reverse=True))
print(tour)
tour.reverse()### 按照列表原来的顺序倒叙排序
print(tour)
(7)使用reverse()再次排序,打印该列表,查看是否已恢复原来的排序
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
print(tour)
print(sorted(tour,reverse=True))
print(tour)
tour.reverse()### 按照列表原来的顺序倒叙排序
print(tour)
tour.reverse()
print(tour)
(8)使用sort()修改列表元素,使其元素按照字母排序,打印该列表,核实排序变了
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
print(tour)
print(sorted(tour,reverse=True))
print(tour)
tour.reverse()### 按照列表原来的顺序倒叙排序
print(tour)
tour.reverse()
print(tour)
tour.sort()
print(tour)
(9)再次使用sort()修改列表,使其元素按照字母相反的顺序排序,打印该列表,核实位置是否变了
tour=['dali','tuerqi','wuerqi','chengdu','xian']
print(tour)
print(sorted(tour))##sorted 对列表临时排序
print(tour)
print(sorted(tour,reverse=True))
print(tour)
tour.reverse()### 按照列表原来的顺序倒叙排序
print(tour)
tour.reverse()
print(tour)
tour.sort()
print(tour)
tour.sort(reverse=True)
print(tour)
###总结:sorted()按照字母临时排序,可使用reverse=True使其倒叙排序,reverse()修改的是列表元素位置倒叙排序,sort()按照字母永久排序,同样也可调用reverse=True
(5)根据题目2中的列表,打印出你共邀请了多少人共进晚餐
names=['liya','liuyang','liyaping','lijinduo']
num=len(names)
print('共邀请',num,'人共进晚餐')
标签:入门,python,list,列表,tour,names,print,sorted,reverse 来源: https://www.cnblogs.com/liya-1995/p/13449588.html