Python自定义一个数组类,支持数组之间的四则运算和其他常见方法
class MyArray:
'''保证输入的内容是整型、浮点型'''
def ___isNumber(self, num):
if not isinstance(num, (int,float)):
return False
return True #开始写构造函数,接受可变长度的数组
def __init__(self, *args):
if args == None:
self.__value = []
else:
for a in args:
if not self.___isNumber(a):
print('All elements must be number!')
#self.__value 是一个数组
self.__value = list(args) #打印输出当前的self.__value
def printSelf(self):
#这个self是一个地址
print(self)
#这个self.__value是一个数组
print(self.__value) #重载len(Array)这个方法
def __len__(self):
return len(self.__value) #append方法
def append(self, other):
self.__value.append(other)
#注意:此处不能够直接return self.__value.append(other)
#这个方法执行后没有返回值
return self.__value #重载运算符+
def __add__(self,other):
if self.___isNumber(other):
#如果other 是一个数,则数组里每一个元素都加上other
array = MyArray()
array.__value = [ i + other for i in self.__value]
return array.__value
elif isinstance(other,MyArray):
#如果other 是一个数组,则两个数组对应位置的数相加
if (len(self.__value) == len(other.__value)):
array = MyArray()
array.__value = [i+j for i,j in zip(self.__value,other.__value)]
return array.__value
else:
print('The size must be equal!')
else:
print('Please input a array or a num!') #重载运算符 / 浮点数除法,返回浮点数
def __truediv__(self,other):
if self.___isNumber(other):
if other == 0:
print("Zero cant be this number!")
return
array = MyArray()
array.__value = [i / other for i in self.__value]
return array.__value
else:
print("It is must be a number except zero!") #重载运算符 // 整数除法,返回不大于结果的最大的一个整数
def __floordiv__(self,other):
if isinstance(other,int):
if other == 0:
print("Zero cant be this number!")
return
array = MyArray()
array.__value = [i // other for i in self.__value]
return array.__value
else:
print("Tt is must be a number except zero!") #重载运算符% 取余数
def __mod__(self,other):
if isinstance(other,int):
if other == 0:
print("Zero cant be this number!")
return
array = MyArray()
array.__value = [i % other for i in self.__value]
return array.__value
else:
print("Tt is must be a number!") #根据数组index查看元素
def __getitem__(self,index):
arrayLength = len(self.__value)
if isinstance(index,int) and (0 <= index <= arrayLength):
return self.__value[index]
else:
print("Index must be a Inteager which is less than", arrayLength-1) #查看元素是否在该列表
def __contains__(self,other):
if other in self.__value:
return True
return False #数组比较
def __lt__(self,other):
if not isinstance(other,MyArray):
print("It is must be the type of MyArray")
return False
if self.__value < other.__value:
return True
return False
Python自定义一个数组类,支持数组之间的四则运算和其他常见方法的更多相关文章
- 2017.12.13 Java中是怎样通过类名,创建一个这个类的数组
先在类方法中定义数组的方法: public int[] method6(int[] arr){ for(int i = 0; i<arr.length;i++){ arr[i] = (int)( ...
- 用python构建一个多维维数组
用python构建一个二维数组 解法? 方法1: num_list=[0]*x//表示位创建一个一维数组为num_lis[x],且数组中的每一项都为0 num_list=[[0]*x for i in ...
- C++自定义String字符串类,支持子串搜索
C++自定义String字符串类 实现了各种基本操作,包括重载+号实现String的拼接 findSubStr函数,也就是寻找目标串在String中的位置,用到了KMP字符串搜索算法. #includ ...
- c++primer,自定义一个复数类
#include<iostream> #include<string> #include<vector> #include<algorithm> #in ...
- Java自定义一个字典类(Dictionary)
标准Java库只包含Dictionary的一个变种,名为:Hashtable.(散列表) Java的散列表具有与AssocArray相同的接口(因为两者都是从Dictionary继承来的).但有一个方 ...
- java中自定义一个异常类 在某些情况抛出自定的异常 ----------阻断程序
//=============定义异常类 package org.springblade.flow.engine.errorException; /** * 自定义异常处理写入sap失败 */ pub ...
- python 分享一个通过 (key1.key2.key3) 形式获取嵌套字典值的方法
最近在做接口自动化测试,响应的内容大多数是多层嵌套的json数据,如果一层层的去剥,效率不高,脚本繁重,所以写了一个可以通过(key1.key2.key3)形式获取嵌套字典值的方法,如有不对或者需要优 ...
- 12.创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方 法初始化x和y。创建类主类A来测试它。
package java1; public class Point { int x; int y; Point(int x,int y) { this.x = x; this.y = y; } pub ...
- 创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方 法初始化x和y。创建类主类A来测试它
package com.hanqi.test; public class Point { private int x; private int y; Point(int xx,int yy) { x= ...
随机推荐
- 064 Minimum Path Sum 最小路径和
给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...
- Apache Atlas是什么?
不多说,直接上干货! Apache Atlas是Hadoop社区为解决Hadoop生态系统的元数据治理问题而产生的开源项目,它为Hadoop集群提供了包括数据分类.集中策略引擎.数据血缘.安全和生命周 ...
- Shell分割字符得到数组
#!/bin/bash p=$(hadoop fs -ls /tgl/data |awk '{print $8}') #要将$a分割开,先存储旧的分隔符 OLD_IFS="$IFS" ...
- ssh登录出现 Host key verification failed. 问题
我们使用ssh链接linux主机时,可能出现“Hostkey verification failed.“的提示,ssh连接不成功.可能的提示信息如下: @@@@@@@@@@@@@@@@@@@@@@@@ ...
- 4. 把一幅彩色图像的R、G、B分量单独显示。
#include <cv.h> #include <highgui.h> int main(void) { IplImage* oo = cvLoadImage(); IplI ...
- mysql 存储过程变量及循环的使用
1.用游标循环 BEGIN -- 定义变量 -- 定义done DECLARE done INT; -- 定义 ammeter_id_bl DECLARE ammeter_id_bl DOUBLE; ...
- Ionic开发-搭建开发环境
1安装node.js 2安装ionic & cordova: 命令行输入:npm install –g cordova ionic 注:-g表示全局安装,也可以进入指定的目录安装,但这里推荐全 ...
- P4868 天天和不可描述
http://www.tyvj.cn/p/4868 思路: 本想用站做的,但发现要用很多站同时做,还要来回倒. 我怕超时,所以换了种做法. 因为每遇到一次括号都要把输出方向改变,而括号是成对存在的,所 ...
- IO流----File,递归,字节流,字符流
要把数据持久化存储,就需要把内存中的数据存储到内存以外的其他持久化设备(硬盘.光盘.U盘等)上. 当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作. 当把持久设备上的数据读 ...
- springboot+shiro+cas实现单点登录之shiro端搭建
github:https://github.com/peterowang/shiro-cas 本文如有配置问题,请查看之前的springboot集成shiro的文章 1.配置ehcache缓存,在re ...