一、元组 tuple
数据形式
(数据1,数据2,数据3.....)
元组数据是不可修改,数据是有序的,支持下标获取数据
无法增加,修改,删除数据,只能进行数据查询
1-1 元组定义
python
# 元组定义
data_tuple1 = (1,2,3,4)
data_tuple2 = (1.2,2.34,3.15,4.26)
data_tuple3 = ('a','b','c','d')
data_tuple4 = (['asd','2213'],[123,3342])
data_tuple5 = ((12,345,74,21),('ndkla','123'))
print(data_tuple1)
print(data_tuple2)
print(data_tuple3)
print(data_tuple4)
print(data_tuple5)
# 元组只有一个元素数据时,需要加逗号
data_tuple6 = (10)
print(type(data_tuple6))
data_tuple7 = ('张三',)
print(type(data_tuple7))
# 元组是有序数据,支持下标
print(data_tuple3[2])
print(data_tuple3[1:4])
# 容器都支持for循环取值
for i in data_tuple5:
print(i)
1-3 元组数据查找
python
# 查找数据
i = data_tuple_str.index('python')
print(i)
# 查找出现的次数
num = data_tuple_str.count('a')
print(num)
# 计算元素个数
print(len(data_tuple_str))
二、字典 dict
2-1 字典定义
python
# 字典定义
data_dict1 = {1:'hadoop',2:'python',3:'spark',4:'hadoop'}
data_dict2 = {'a':'hadoop','b':'python','c':'spark','d':'hadoop'}
print(data_dict1)
print(data_dict2)
# 字典中不要重复定义key值,保证key值唯一
data_dict3 = {'a':'hadoop','b':'python','c':'spark','a':'flink'}
print(data_dict3)
# 使用key的下标值进行value部分取值
print(data_dict1[2])
print(data_dict2['d'])
# 字典下标不支持切片操作
# print(data_dict1[1:4])
2-2 字典取值
字典的内容包含两部分 key 和 value
在字典取值时可以分别取出两部分的数据
-
取value部分的数据
-
方式1 通过key取value 一次只能取一个value值
-
data[key]
-
data.get(key)
-
-
方式二 获取所有的value值
- data.values()
-
python
# 取字典的value值
data_dict2 = {'a':'hadoop','b':'python','c':'spark','d':'hadoop'}
# 通过key取value
# 使用[] 取值,如果key不存在会报错
res = data_dict2['a']
print(res)
# get取值时 key不存在会返回一个None值
res2 = data_dict2.get('z')
print(res2)
# 将字典中value值全部取出
res3 = data_dict2.values()
print(res3)
取key值部分
-
获取所有key值
-
data.keys() 是的
python
# 获取字典中的所有key值
res4 = data_dict2.keys()
print(res4)
2-3 字典的增加和修改数据
python
# 增加或修改字典数据
data_dict = {'id':1,'name':'张三','age':20,'gender':'男'}
print(data_dict)
# key值存在,修改对应的value数据
data_dict['age'] = 22
print(data_dict)
# key不存在,则增加薪数据
data_dict['hobby'] = '篮球'
print(data_dict)
2-4 字典删除
python
# 删除字段数据
# 通过key确定删除的数据
data_dict = {'id':1,'name':'张三','age':20,'gender':'男'}
print(data_dict)
del data_dict['name']
print(data_dict)
# 清空数据
data_dict.clear()
print(data_dict)
2-5 for循环遍历
python
# 字典的for循环遍历
data_dict = {'id':1,'name':'张三','age':20,'gender':'男'}
for k,v in data_dict.items():
print(k,v)
三、集合
3-1 集合的定义
python
# 集合的定义
data_set1 = {1,22,13,4}
data_set2 = {1.1,2.35,3.16,4.28}
data_set3 = {'a','b','c','s','a','a'}
# data_set4 = {['a','b'],['c','s']}
# data_set5 = {{'id':1,'name':'张三'},{'id':2,'name':'李四'}}
data_set6 = {(1,2,3),('a','b')}
# data_set7 = {{1,2,3},{'a','b','c'}}
print(data_set1)
print(data_set2)
print(data_set3)
# print(data_set4)
# print(data_set5)
print(data_set6)
# print(data_set7)
3-2 集合增删改
python
# 增加的数据
data_set.add('hive')
print(data_set)
# 删除数据
data_set.remove('hadoop')
print(data_set)
# 更新数据
data_set.update(['flink222'])
print(data_set)
四、公共方法
4-1 容器类型之间的转化
python
# 容器类型之间的转化
data_str = 'itcast'
data_list = ['hadoop','hive','spark','python','list','python']
data_tupe = ('hadoop','hive','spark','python','tuple','hadoop')
data_set = {'hadoop','hive','spark','python','set'}
# 字符串
print(list(data_str))
print(tuple(data_str))
print(set(data_str))
# 元组操作
print(str(data_tupe))
print(list(data_tupe))
print(set(data_tupe))
# 列表
print(str(data_list))
print(tuple(data_list))
print(set(data_list))
4-2 运算符
python
# + *
data_str1 = 'itcast'
data_str2 = 'python'
data_list1 = ['a', 'b']
data_list2 = ['c', 'd']
data_tuple1 = ('w', 'q')
data_tuple2 = ('e', 'f')
data_set1 = {1, 2, 3}
data_set2 = {4, 5, 6}
data_dict1 = {'id': 1, 'name': '张三'}
data_dict2 = {'id': 2, 'name': '李四'}
# + 运算符
data_str1_str2 = data_str1 + data_str2 # 字符串+ 是拼接字符串
print(data_str1_str2)
data_list1_list2 = data_list1 + data_list2 # 列表+ 合并列表数据
print(data_list1_list2)
data_tuple1_tuple2 = data_tuple1 + data_tuple2 # 元组+ 合并元组数据
print(data_tuple1_tuple2)
# 集合数据不支持 + 运算
# data_set1_set2 = data_set1+data_set2
# print(data_set1_set2)
# 字典不支持+运算
# * 运算符 数据 * 数值
data_str1_new = data_str1 * 3
print(data_str1_new)
data_list1_new = data_list1 * 3
print(data_list1_new)
data_tuple1_new = data_tuple1 * 3
print(data_tuple1_new)
# 集合和字典不支持 * 运算符
# data_set1_new = data_set1 * 3
# print(data_set1_new)
# 判断元素是否在容器内
if 'i' in data_str1:
print('在字符串中')
else:
print('不在字符串中')
if 'a' in data_list1:
print('在列表中')
else:
print('不在列表中')
if 'a' in data_set1:
print('在集合中')
else:
print('不在集合中')
# 判断key是否在字典中
if 'id' in data_dict1:
print('key在列表中')
else:
print('key不在列表中')