Python中数组的几种使用方法

来源:互联网 发布:不锈钢开孔器淘宝网 编辑:程序博客网 时间:2024/06/03 02:26

二维数组的初始化

matirx_done = [[0 for i in range(0, len(matirx))]for j in range(0, len(matirx[0]))]

就将其初始化为一个与matrix相同大小的元素全为 0 的矩阵

数组的多级排序

在数组 idea_collect = [[3, 1, 2], [3, 2, 1], [3, 2, 2], [3, 1, 1]] 中, 先按照第二项排列, 再按照第三项倒序排列 可写为:

idea_collect.sort(key=lambda x: (x[1], -x[2]))

其中, x[1] 代表第二项正序排列, -x[2] 代表第三项倒序排列

排列结果为 [[3, 1, 2], [3, 1, 1], [3, 2, 2], [3, 2, 1]]

在一个 class 中多个函数不传参使用同一个数组

如例所示:

class Partition:    def __init__(self):        self.num_complete = []    def partition(self, num, start, end):        self.num_compelete = num      def partition_core(self):        del self.num_compelete[0] 

其中,self.num_compelete就是 class 中两个函数同时可以直接调用的数组, 不过最好先在def __init__中声明这个数组

原创粉丝点击