Python编程:从入门到实践------第3章:列表

一、修改、添加和删除元素

讨论如何对列表中元素进行添加、删除及修改

1.添加元素

(1)在列表末尾添加元素——append

输入如下代码

name=["liming","zhanghua","chenli"]
print(name)

name.append("wangcan")
print(name)

输出结果如下

['liming', 'zhanghua', 'chenli']
['liming', 'zhanghua', 'chenli', 'wangcan']

(2)在列表中插入元素——insert

输入如下代码:

name=["liming","zhanghua","chenli"]
print(name)

name.insert(1,'wangcan')
print(name)

输出结果如下图:

['liming', 'zhanghua', 'chenli']
['liming', 'wangcan', 'zhanghua', 'chenli']

2.删除元素

(1)使用del语句删除元素(已知位置)

name=["liming","zhanghua","chenli"]
print(name)

del name[0]
print(name)

则会输出如下结果:

['liming', 'zhanghua', 'chenli']
['zhanghua', 'chenli']

(2)使用方法pop删除元素

方法pop()可删除列表末尾的元素,并能让你接着使用它。

name=["liming","zhanghua","chenli"]
print(name)

popped_name=name.pop()
print(name)
print(popped_name)

输出如下

['liming', 'zhanghua', 'chenli']
['liming', 'zhanghua']
chenli

(3)弹出列表中任何位置处的元素

可用pop删除列表中任何位置的元素,只需在括号中加入索引

name=["liming","zhanghua","chenli"]
print(name)

popped_name=name.pop(0)
print(name)
print(popped_name)

输出如下:

['liming', 'zhanghua', 'chenli']
['zhanghua', 'chenli']
liming

(4)根据值删除元素(位置未知)利用方法remove()

执行以下代码:

name = ["liming", "zhanghua", "chenli"]
print(name)

name.remove("liming")
print(name)

输出如下:

['liming', 'zhanghua', 'chenli']
['zhanghua', 'chenli']

动手试一试 3-4到3-7代码

name=["Zhangsan","Lisi","Wangwu"]
invitation1=name[0]+","+"welcome to my party"+"!"
invitation2=name[1]+","+"welcome to my party"+"!"
invitation3=name[2]+","+"welcome to my party"+"!"

print(invitation1)
print(invitation2)
print(invitation3)

print("\n")
explain1="Zhangsan can't come."
print(explain1)
name[0]="chenliu"
invitation1=name[0]+","+"welcome to my party"+"!"
invitation2=name[1]+","+"welcome to my party"+"!"
invitation3=name[2]+","+"welcome to my party"+"!"
print(invitation1)
print(invitation2)
print(invitation3)

print("\n")
print("I find a bigger table")
name.insert(0,"a")
name.insert(2,"b")
name.append('c')

invitation1=name[0]+","+"welcome to my party"+"!"
invitation2=name[1]+","+"welcome to my party"+"!"
invitation3=name[2]+","+"welcome to my party"+"!"
invitation4=name[3]+","+"welcome to my party"+"!"
invitation5=name[4]+","+"welcome to my party"+"!"
invitation6=name[5]+","+"welcome to my party"+"!"

print(invitation1)
print(invitation2)
print(invitation3)
print(invitation4)
print(invitation5)
print(invitation6)

print("\n")
print("no new table,just two guests")
popped_name=name.pop()
print(popped_name+",sorry,you can not come")
popped_name=name.pop()
print(popped_name+",sorry,you can not come")
popped_name=name.pop()
print(popped_name+",sorry,you can not come")
popped_name=name.pop()
print(popped_name+",sorry,you can not come")
print(name[0]+",you are still in the list")
print(name[1]+",you are still in the list")
del name[0]
del name[0]
print(name)

二、组织列表

1.使用方法sort()对列表进行永久性排序

cars=['bmw','audi','toyota','subaru']
cars.sort()
print(cars)

列表则按首字母排序,且无法恢复。

['audi', 'bmw', 'subaru', 'toyota']

若在sort中传递参数reverse=True,则按与字母顺序相反顺序排列

cars=['bmw','audi','toyota','subaru']
cars.sort(reverse=True)
print(cars)

输出结果如下

['toyota', 'subaru', 'bmw', 'audi']

2.使用函数sorted()对列表进行临时排序

sorted()可保留原排序,同时以特定方式呈现他们.

cars = ['bmw', 'audi', 'toyota', 'subaru']
print(sorted(cars))
print(cars)

输出:

['audi', 'bmw', 'subaru', 'toyota']
['bmw', 'audi', 'toyota', 'subaru']

3.倒着打印列表——使用方法reverse()

cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)

cars.reverse()
print(cars)

输出结果如下:

['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']

4.确定列表长度——使用函数len()

cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))

输出:

4

动手试一试代码:

cities=['France','Iceland','New Zealand','America','Britain']
print(cities)
print(sorted(cities))
print(cities)
print(sorted(cities,reverse=True))
print(cities)
cities.reverse()
print(cities)
cities.reverse()
print(cities)
cities.sort()
print(cities)
cities.sort(reverse=True)
print(cities)

输出:

['France', 'Iceland', 'New Zealand', 'America', 'Britain']
['America', 'Britain', 'France', 'Iceland', 'New Zealand']
['France', 'Iceland', 'New Zealand', 'America', 'Britain']
['New Zealand', 'Iceland', 'France', 'Britain', 'America']
['France', 'Iceland', 'New Zealand', 'America', 'Britain']
['Britain', 'America', 'New Zealand', 'Iceland', 'France']
['France', 'Iceland', 'New Zealand', 'America', 'Britain']
['America', 'Britain', 'France', 'Iceland', 'New Zealand']
['New Zealand', 'Iceland', 'France', 'Britain', 'America']
发布了8 篇原创文章 · 获赞 0 · 访问量 442
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览