列表
基本概念
-
列表包含一系列元素,这些元素按照一定的顺序排列,元素可以是任何东西,通常以名词的复数形式命名,用[]将元素包含起来
boxes = ['ball','pen',990]
-
列元素的访问只需要将元素的位置(索引)告诉python即可,访问时指出列表的名称以及索引号即可访问,注意元素在列表内的排列序号是从0开始,并且在不知列表长度时可使用索引号[-1]表示访问列表最后一个元素,[-2]访问列表倒数第二个元素,以此类推
boxes = ['ball','pen',990,89.5] boxes[0] ball boxes[2] 990 boxes[-1] 89.5 boxes[-2] 990
-
列表内元素的使用需要借助元素的索引来完成
boxes = ['ball','pen',990,89.5] print('I have a ' + boxes[0])
课后习题
3-1姓名
names = ['john','mike','hank']
names[0]
john
names[1]
mike
names[2]
hank
3-2问候语
names = ['john','mike','hank']
print('hello,' + names[0])
hello,john
3-3自己的列表
ways = ['motorcycle','car','bicycle']
print('I would like to own a ' + ways[1])
I would like to own a car
基本操作(基于动态列表)
修改元素
指定元素的修改和修改变量的值一样,将要修改的值赋值给相应元素即可
ways = ['motorcycle','car','bicycle']
ways[2] = 'bus'
print(ways)
['motorcycle','car','bus']
添加元素
使用.append()动态地在列表末尾添加元素,这里可以看出该方法只能添加一个元素
ways = ['motorcycle','car','bicycle']
ways.append('train')
print(ways)
['motorcycle','car','bicycle','train']
插入元素
使用insert在指定的位置插入相应的元素,用法为列表.insert(index,value)
ways = ['motorcycle','car','bicycle']
ways.insert(2,'airplane')
print(ways)
['motorcycle','car','airplane','bicycle']
删除元素
如果不知列表中元素的位置,只知道值,可使用remove方法删除,用法为列表.remove(value);如果知道列表中元素的位置可使用del和pop()用法为del 列表[index],列表.pop(index)
- del 删除列表中的元素之后就不可以再次使用该元素
- pop 括号中没有index就是删除最后一个元素,删除元素后还可以继续使用该元素
ways = ['motorcycle','car','airplane','bicycle']
ways.remove('airplane')
print(ways)
['motorcycle','car','bicycle']
del ways[1]
print(ways)
['motorcycle','bicycle']
poped_ways = ways.pop()
print(ways)
['motorcycle']
print(poped_ways)
['bicycle']
课后习题
lists = ['john','mike','hank'] # 建立嘉宾名单
print('Dear ' + lists[0] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[1] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[2] + ':' + '\n\tI want to invite you to my party')
print(list[0] + ' can not join the party')
lists.remove('john') #移除不能来的嘉宾
lists[0] = 'leo' # 修改嘉宾名单
print('Dear ' + lists[0] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[1] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[2] + ':' + '\n\tI want to invite you to my party')
print('I find a bigger table')
lists.insert(0,'charlie') # 添加到名单开头
lists.insert(2,'kurt')
lists.append('tex') # 添加到名单末尾
print('Dear ' + lists[0] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[1] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[2] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[3] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[4] + ':' + '\n\tI want to invite you to my party')
print('Dear ' + lists[5] + ':' + '\n\tI want to invite you to my party')
poped_lists1 = lists.pop() # 使用pop删除名单人员
print('Dear ' + poped_lists1 + ':' + '\n\tI\'m sorry that you can\'t to the party')
poped_lists2 = lists.pop()
print('Dear ' + poped_lists2 + ':' + '\n\tI\'m sorry that you can\'t to the party')
poped_lists3 = lists.pop()
print('Dear ' + poped_lists3 + ':' + '\n\tI\'m sorry that you can\'t to the party')
poped_lists4 = lists.pop()
print('Dear ' + poped_lists4 + ':' + '\n\tI\'m sorry that you can\'t to the party')
print('Dear ' + lists[0] + ':' + '\n\t attend on the party on time please')
print('Dear ' + lists[1] + ':' + '\n\t attend on the party on time please')
del lists[0]
del lists[0] # 第一次删除之后索引为1的元素索引变为0
print(lists)
列表的组织操作
永久性排序
采用方法sort()对原列表直接进行修改,默认按照升序进行排列,倒序则在括号内添加参数reverse = True
traffic = ['bus','car','airplane']
traffic.sort()
print(traffic)
['airplane','bus','car']
traffic.sort(reverse = True) # 倒序
['car','bus','airplane']
临时性排序
采用**函数sorted()**对产生新列表,该列表为排序后的列表,原列表不变化,倒序则在括号内添加参数reverse = True
traffic = ['bus','car','airplane']
print(sorted(traffic))
['airplane','bus','car']
print(traffic)
['bus','car','airplane']
print(sorted(traffic,reverse=True))
['car','bus','airplane']
倒着打印列表
方法reverse(),注意是对列表中的元素以相反的顺序进行排序是永久性排序,但是可再次reverse恢复原序列
traffic = ['bus','car','airplane']
traffic.reverse()
print(traffic)
traffic = ['airplane','car','bus']
traffic.reverse() #再次reverse恢复原序列
print(traffic)
['bus','car','airplane']
列表长度-len()获取列表长度,即元素个数
课后习题
3-8放眼看世界
lists = ['Paris','London','NewYork','shanghai','Berlin']
print(list)
print(sorted(lists)) # 打印使用函数sort后的列表
print(lists) # 打印原列表
print(sorted(lists,reverse=True)) # 以相反序列使用函数sort打印列表
print(lists) # 打印原列表
lists.reverse() # 列表倒序
print(lists) # 打印倒序后的列表
lists.reverse() #再次倒序
print(lists) # 打印再次倒序后的列表
lists.sort() # 使用方法sort对列表排序
print(lists) # 打印排序后的列表
lists.sort(reverse = True) # 将sort排序后的列表逆序
print(lists) # 打印逆序后的列表
3-10尝试使用各个函数
world = ['China','huangshan','Yangtze','xiamen','python']
print(sorted(world)) # 函数sorted排序
len(world) # 函数len计算长度
小白入门python,如有错误,欢迎纠正与讨论