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

python入门——列表简介的相关练习

2018-03-12 17:25 686 查看
1.元素的增删
friends = [ 'alice', 'bob', 'cindy', 'Tom']
for person in friends:
print(person.title()+',how are you?\n')

print( friends[1].title()+' is not here, but lucy have come here.\n')
friends[1] = 'lucy'

print('See, xiaoming comes here too!\n')
friends.insert(0,'xiaoming')

print("Wo, is that nancy?\n")
friends.insert(int(len(friends)/2),'nancy')

print('And that girl who is singing is xiaohong.\n')
friends.append('xiaohong')

print('party is over!\n')

friends.pop()
friends.pop()
friends.pop()
friends.pop()

del friends[-1]
del friends[-1]

friends.remove('xiaoming')

print(friends)
输出结果:



2.列表排序
plance = [ 'kunming', 'xihu', 'shanghai', 'beijing', 'chendu']
print(plance)
print('\n')
print(sorted(plance))
print('\n')
print(plance)
print('\n')
print(sorted(plance,reverse=True))
print('\n')
plance.reverse()
print(plance)
plance.reverse()
print(plance)
plance.sort()

print(plance)

输出结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: