Python 列表、元组、集合、字典、Json、数组、矩阵 简单示例说明
2017-12-13 11:39
846 查看
【列表】有序的集合,可随时添加和删除其中的元素
【元组】tuple 和 list 非常类似,但tuple不能修改(约束强)
【集合】不重复
【字典】
其他应用:
【数组】——【矩阵*假】
【矩阵*真】
【列表】有序的集合,可随时添加和删除其中的元素 list = [1,2,3,'A','B','C'] #查看 list list[5] print(list[5]) #末尾添加 list.append('D') #指定位置之后添加 list.insert(1, '1.5') #删除最后一个 list.pop() #删除指定值 list.remove('C') #删除指定位置的 list.pop(1) #遍历查看 for v in list: print(v) for i in range(len(list)): print ("index = %s,value = %s" % (i, list[i])) for i, v in enumerate(list): print ("index = %s,value = %s" % (i, v))
【元组】tuple 和 list 非常类似,但tuple不能修改(约束强)
【元组】tuple 和 list 非常类似,但tuple不能修改(约束强) tuple = (1,2,3,'A','B','C') tuple = ([1,2,3,'A','B','C'],4,'D') list = ([1,2,3,'A','B','C']) #元组中只有1个列表,这种写法结果变成了列表,不是元组,也不是集合 #查看 tuple tuple[5] print(tuple[5]) #碎不能直接改,但可与其他方法添加 tuple = tuple + ('D',) #删除元组 del tuple #遍历查看(与列表一样) for v in tuple: print(v) for i in range(len(tuple)): print ("index = %s,value = %s" % (i, tuple[i])) for i, v in enumerate(tuple): print ("index = %s,value = %s" % (i, v))
【集合】不重复
【集合】不重复 myset = set([1,2,3,'A','B','C']) myset2 = set([1,2,'B','C']) #查看 myset print(myset) #添加 myset.add('D') #删除 myset.remove('D') #移除最早值 myset.pop() #并集 myset | myset2 #交集 myset & myset2 #差集 myset - myset2 #遍历 for v in myset: print (v) for i, v in enumerate(myset): print ("index = %s,value = %s" % (i, v))
【字典】
dict = {} dict = {'name':'kk', 'age':100} #查看 dict dict['name'] print(dict['age']) print(dict.keys()) #修改 dict['age'] = 18 #添加 dict['birthday'] = '1900-01-01' #删除键值对 del dict['birthday'] #遍历 for k in dict: print('%s = %s' % (k,dict[k])) for k in dict.keys(): print('%s = %s' % (k,dict[k])) for k,v in dict.items(): print('%s = %s' % (k,v))
其他应用:
【json】 #格式:字典 aa = {"name": "kk", "age": 99 } #格式:列表中存储字典 bb = [ {"name": "mm", "age": 10},{"name": "qq", "age": 20},{"name": "hh", "age": 50} ] #遍历列表 for item in bb: print(item,item["name"]) #Json格式化 import json print(json.dumps(aa,indent=4)) print(json.dumps(bb,indent=4)) 【多维/嵌套列表】——【矩阵*假】 格式:列表中保存格式一致的列表 list_matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ] #查看 print(list_matrix[2]) print(list_matrix[2][0])
【数组】——【矩阵*假】
from numpy import * #数组 arr1=array([2,2,2]) arr2=array([2,3,5]) #数组对应相加/相乘 arr3 = arr1 + arr2 arr4 = arr1 * arr2 print(arr3) print(arr4) #多维数组 arr5=array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print(arr5) print(arr5[1,0]) print(arr5[1][0])
【矩阵*真】
【矩阵*真】 from numpy import * mat0 = matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print(mat0) #嵌套列表转为矩阵 list = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],] mat1=matrix(list) print(mat1) #数组转为矩阵 arr5=array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) mat2=matrix(arr5) print(mat2) #3行4列值为1的矩阵 mat2=matrix(ones((3,4))) print(mat2) #对角矩阵 mat2=matrix(eye(4,4,dtype=int)) print(mat2)
相关文章推荐
- python的列表、元组、序列、字典、集合的简单说明
- Python——“元组、列表、字典、数组和矩阵”的大小问题
- python 列表,元组,字典,集合简单总结
- Python 列表 元组 字典 集合
- 《Python语言及其应用》读书笔记(2)之第3章Python容器:列表、元组、字典与集合(关键词:列表/元组/字典/集合)
- python的列表(List),元组(Tuple),集合(Set),字典(Dict)
- Python中几种数据结构的整理,列表、字典、元组、集合
- python的列表,元组,字典,序列,字符串的简单解说
- python学习-1-列表,元组,字典,集合,文件操作
- Python基础 -- 02 基础语法(字符串、列表、元组、字典、set集合)
- python中列表 元组 字典 集合的区别
- Python 学习笔记 -- 变量、元组、列表、字典和集合
- Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)
- Python总结(字符串、列表、元组、字典、集合和文件)
- [置顶] Python联系笔记2[列表,元组,集合,字典,函数,文件操作,模块,类]
- 【277】◀▶ Python 列表/元组/字典说明
- 无限遍历,python实现在多维嵌套字典、列表、元组的JSON中获取数据
- python学习-1-列表,元组,字典,集合,文件操作
- Python中列表/元组/集合/字典的理解
- python基础(09)——列表,元组,字典,集合