元组和元组解包
tuple
元组名=( , )
>>> place=(1,2)
>>> print('Longitude:',place[0])
Longitude: 1
>>> print('Latitude:',place[1])
Latitude: 2
元组解包--批量数据批量赋值
>>> dimensions=1,2,3
>>> length,width,hight=dimensions
>>> length
1
>>> width
2
>>> hight
3
>>> length,width,hight=dimensions
>>>print('The dimensions are'+str(length)+'*'+str(width)+'*'+str(hight))
The dimensions are1*2*3
>>>print('The dimensions are {}*{}*{}'.format(length,width,hight))
The dimensions are 1*2*3
集合
集合和列表去重
- 使用
set
函数将列表转换为集合的形式,并进行去重
>>> place=['Beijing','Shanghai','shanxi','Xian','shanxi','Beijing']
>>> unique_place=set(place)
>>> print(unique_place)
{'Beijing', 'Xian', 'shanxi', 'Shanghai'}
集合中加入或删除一个元素
- 加入--
add()
- 删除--
pop()
(在集合中随机删除,不可指定位置,数组中可指定位置删除)
>>> place
{'Beijing', 'Xian', 'shanxi', 'Shanghai'}
>>> place.add('Shenzhen')
>>> print(place)
{'Shenzhen', 'shanxi', 'Beijing', 'Xian', 'Shanghai'}
>>> place.pop()
'Beijing'
>>> print(place)
{'Xian', 'shanxi', 'Shanghai'}