Python 列表、元组、集合、字典、Json、数组、矩阵 简单示例说明
来源:互联网 发布:花生壳的域名ping不通 编辑:程序博客网 时间:2024/06/05 22:02
【列表】有序的集合,可随时添加和删除其中的元素
【列表】有序的集合,可随时添加和删除其中的元素list = [1,2,3,'A','B','C']#查看listlist[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个列表,这种写法结果变成了列表,不是元组,也不是集合#查看tupletuple[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'])#查看mysetprint(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}#查看dictdict['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 jsonprint(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 + arr2arr4 = arr1 * arr2print(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)
阅读全文
0 0
- Python 列表、元组、集合、字典、Json、数组、矩阵 简单示例说明
- python的列表,元组,集合,字典
- 元组 列表 集合 字典 python
- python的列表、元组、字典、集合
- Python 列表、元组、字典、集合
- Python 列表、元组、字典、集合 区别
- Python 列表 元组 字典 集合
- Python——“元组、列表、字典、数组和矩阵”的大小问题
- 列表、元组、字典、集合
- Python 学习笔记 -- 变量、元组、列表、字典和集合
- python入门:列表、元组、字典及集合类型
- Python中列表、字典、元组、集合数据结构整理
- Python中列表/元组/集合/字典的理解
- python中列表、元组、字典和集合
- Python学习笔记二:列表、字典、元组、集合 简介
- python笔记2 (列表,元组,字典,集合)
- python学习-1-列表,元组,字典,集合,文件操作
- 理解Python中列表,元组,字典,集合的区别
- 数组的全面总结
- Java的三种代理模式
- joern代码属性图简介
- Koltin——最详细的可见性修饰符详解
- css3自适应布局的calc函数使用
- Python 列表、元组、集合、字典、Json、数组、矩阵 简单示例说明
- springmvc工作原理
- WCF服务编程(2)——自托管服务
- 历史笔记更新计划
- Fiori学习笔记
- 高数 08.02 可分离变量微分方程
- 在线网络投票活动实施步骤及疑难问题汇编[制作中...]
- 求一个数的绝对值
- 解决ubuntu的apt-get命令被占用