Python_C3_变量【下】

来源:互联网 发布:网络红歌大全 编辑:程序博客网 时间:2024/05/05 12:01

3.3.3 通过附加序列增长列表

不能用append方法,它会向列表中添加分层的序列;

可以用extend方法。

>>> living_room=("rug","table","chair","TV","dustbin","shelf")>>> apartment=[]>>> apartment.append(living_room)>>> print(apartment)[('rug', 'table', 'chair', 'TV', 'dustbin', 'shelf')]   # 使用append方法,得到的分层序列>>> apartment1=[]>>> apartment1.extend(living_room)>>> print(apartment1)['rug', 'table', 'chair', 'TV', 'dustbin', 'shelf']     #正确结果 
3.3.4 使用列表临时存储数据

使用pop方法弹出列表中的元素,pop(0)、pop(1)......

>>> todays_temperatures=[23,32,33,31]>>> todays_temperatures.append(29)>>> todays_temperatures[23, 32, 33, 31, 29]>>> morning=todays_temperatures.pop(0)>>> print("This mornings temperature was %0.2f"% morning)This mornings temperature was 23.00>>> late_morning=todays_temperatures.pop(0)>>> late_morning32>>> noon=todays_temperatures.pop(0)>>> noon33>>> todays_temperatures[31, 29]
说明:如果不告诉pop要删除列表中的那个元素,则把列表中最后一个删除。

3.3.5 处理集合

集合与字典类似,只是仅包含键,而没有与键相关联的值

在本质上,集合是不包含重复数据的数据集。

用途之一:可以用于从数据集中删除重复数据

>>> alphabet=['a','b','c','d','a','f','g','d']>>> alphabet['a', 'b', 'c', 'd', 'a', 'f', 'g', 'd']>>> alph2=set(alphabet)           #使用set>>> alph2{'d', 'f', 'g', 'a', 'b', 'c'}



0 0
原创粉丝点击