元组、列表、字典、集合
相同点:
都可通过索引及切片方式访问某一部分数据。
-索引从末尾开始切片
t=(1,2,3,4,5)
t[2:3] ------>3 #[起点索引:终点索引但不包括自身]
l=[1,2,3,4,5]
l[2:3] -------->3
不同点:
元组内元素不支持修改,()号表示
列表[]号表示,可用append(),在末尾元素添加新元素
字典{}号表示
集合内是一个无序且不重复元素序列,set()表示
字典可单独访问KEY 或 VALVE
相互转化
字符串转列表
strs='hello,world'
strs_list=strs.split(',') #,为截取标志
['hello','world']
列表转元组
strs_tuple=tuple(strs.list) #列表转元组
('hello','world')
元组转列表
strs_lists=list[str_tuple] #元组转列表
['hello','world']
列表转字典
strs_dict=dict(zip(strs_lists,strs_list)) #列表转字典,strs_lists为KEY,strs_list为VALVE
{'hello':'hello','world','world'}
列表转集合
strs_set=set(strs_lists*2) #列表转集合,strs_list*2形成两个['hello','world','hello','world']
{'world','hello'} #顺序随机
字典的访问
strs_dict.item() KEY & VALUE访问
dict_items([('hello', 'hello'), ('world', 'world')])
strs_dict.keys() KEY访问
dict_keys(['hello', 'world'])
strs_dict.values() VALUE访问
dict_values(['hello', 'world'])