playlist for today: Super Junior Balled Songs
Tasks
- 什么是列表
- 如何使用列表元素
3.1 What is list
consist of: 一系列按照特定顺序排列的元素
用[]
表示列表,用,
分隔列表中的元素
定义和输出列表
tnt = ['mjq','dcx','syx','lyw','zzy','yhx','hjl']
print(tnt)
访问列表元素
列表->有序集合,访问元素->指出列表名称和元素索引
#3.1.1
print(tnt[1])
print(tnt[1].upper())
print("the back of sdsnt is " + tnt[-1].upper())
索引从0开始
3.2 修改、添加和删除元素
列表大多数是动态的
修改列表元素
指定列表名和元素的索引,再指定新值
print(tnt)
tnt[0] = 'magic'
print(tnt)
添加元素
append()
method - 在末尾添加
tnt.append('zzx')
print(tnt)
tnt = []
tnt.append('mjq')
tnt.append('dcx')
tnt.append('syx')
tnt.append('lyw')
tnt.append('zzy')
tnt.append('yhx')
tnt.append('hjl')
print(tnt)
insert()
method - 插入元素
tnt.insert(0,'sdsnt')
print(tnt)
删除元素
del
语句 - 知道元素的位置的情况pop()
method - 弹出列表末尾元素,并储存至另一list- 类比:列表->栈,删除列表末尾的元素->弹出栈顶元素
pop
method - 弹出位于任何位置的元素remove()
method - 根据值删除元素- 只能删除第一个指定的值,如果指定值反复出现,则需要使用循环语句
# delete
del tnt[0]
print(tnt)
# pop
tnt_back = tnt.pop() #save the poped value into a new list
print(tnt)
print("back of tnt is " + tnt_back)
tnt_top = tnt.pop(0)
print(tnt)
print("top of tnt is " + tnt_top)
# remove
tnt.remove('yhx')
print(tnt)
tnt = ['mjq','dcx','syx','lyw','zzy','yhx','hjl']
print(tnt)
back_n_forth = 'yhx'
tnt.remove(back_n_forth)
print('\n' + back_n_forth.upper() + " is remove as a traitor")
practice
# practice
# hostess = input("your name please: ")
hostess = 'syx'
guests = [tnt[0], tnt[3],'azy']
print('\nORIGINAL GUESTS-------\n' + str(guests))
print('\nINVITATIONS FROM ' + hostess + '-------')
msg1 = ('hi ')
msg2 = (', this is ' + hostess + ',\n\twould you like to throw a party tomorrow? It would be great if you could join us. \n\t<3.')
for guest in guests:
print(msg1 + guest.upper() + msg2)
msg3 = (' is busy with work.')
busy = 'azy'
guests.remove(busy)
print(busy.upper() + msg3)
guests.append('zzy')
print('\nNEW GUESTS------\n' + str(guests))
print('\nNEW INVITATIONS FROM ' + hostess + '-------')
for guest in guests:
print(msg1 + guest.upper()+ msg2)
print("\nI've book a new bar, so we can have more guests")
guestsnew = ['dcx', 'yhx', 'hjl']
guests.insert(1,'dcx')
guests.insert(5,'yhx')
guests.append('hjl')
print('NEW GUESTS FROM TNT-------\n' + str(guests))
print('\nNEW INVITATIONS FOR TNT-------')
for guest in guests:
print(msg1 + guest.upper()+ msg2)
#
print('\nWe can have only two guests in the party')
print('APOLOGISE-------')
while len(guests) > 2:
poped = guests.pop()
# print(guests)
msg4 = ('Sorry, ')
msg5 = (". \n\tThe party is cancelled, let's hang out together next time when you are free.")
print(msg4 + poped.upper() + msg5)
print('\nINVITATIONS-------')
for guest in guests:
print(msg1 + guest.upper() + msg2)
print('\nthe length of guests is ' + str(len(guests)))
3.3 组织列表
sort()
method永久性排序
sorted()
function临时排序
reverse()
method翻转列表元素顺序
len()
function确定列表长度
code
# 3.3 organize
tnt = ['mjq','dcx','syx','lyw','zzy','yhx','hjl']
print(tnt)
guests = tnt
print('\n' + str(guests))
guests.sort()
# the sort of tnt is changed as well, why?
print('ascending alphabetical: ' + str(guests))
guests.sort(reverse = True)
print('descending alphabetical: '+ str(guests))
tnt = ['mjq','dcx','syx','lyw','zzy','yhx','hjl']
guests = tnt
print('\nalphabetical list: '+ str(sorted(guests)))
print('reverse alpha list: ' + str(sorted(guests, reverse=True)))
print('the original list: ' + str(guests))
tnt.reverse()
print('\nreverse TNT is:')
print(tnt)
len(tnt)
print('\nThere are ' +str(len(tnt)) + ' members in TNT')
3.4 避免索引错误
- python索引时,从0计数
to sum up
method:
append()
, insert()
,
pop()
, remove()
,
sort()
, reverse()
sentence:
del
function:
sorted()
, len()