赞
踩
该部分将对python数据结构、函数等基础内容进行回顾,python大牛和想要直接套用模板进行数据分析方法的朋友可以直接跳过此部分。
元组和列表是python最常见也是最基本的数据结构,其区别在于元组的内容和长度是不可变的,而列表是可变的。下面通过实例来介绍一些元组列表的基本操作。
- a=[1,2,3,4]
- b=1,2,3,4
- print(type(a))
- print(type(b))
- <class 'list'>
- <class 'tuple'>
(1)列表->元组
- a=tuple(a)
- print(a)
- print(type(a))
- (1, 2, 3, 4)
- <class 'tuple'>
(2)元组->列表
- b=list(b)
- print(b)
- print(type(b))
- [1, 2, 3, 4]
- <class 'list'>
可以通过tuple和list函数将任意序列和迭代器换成元组和数列。
元组和列表扩展的方式是相同的。
- (1,2,"jerry")+(3,4,"tom")
- (1, 2, 'jerry', 3, 4, 'tom')
- [1,2,"jerry"]+[3,4,"tom"]
- [1, 2, 'jerry', 3, 4, 'tom']
- (1,"jerry")*2
- (1, 'jerry', 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。