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= ...
随机推荐
- hdu5726-GCD-ST表+二分
先用st表处理出所有l-r的GCD值,然后二分求得这些值一共出现了多少次. #include<bits/stdc++.h> #define inf 0x3f3f3f3f ; using n ...
- NET Core使用Quartz
NET Core使用Quartz 一.前言运用场景 Quartz.Net是一个强大.开源.轻量的作业调度框架,在平时的项目开发当中也会时不时的需要运用到定时调度方面的功能,例如每日凌晨需要统计前一天的 ...
- airodump-ng 界面参数比较详细的解释
BSSID: AP(access point)的MAC地址,,如果在client section中BSSID显示为"not associated" ,那么意味着该客户端没有和AP连 ...
- PHP正则表达式 - 附录(常用正则表达式)
常用正则表达式附录I 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: "^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1- ...
- go 从表结构生成结构体
package main import ( "fmt" "github.com/gohouse/converter" ) func main() { // 初始 ...
- SpringBoot 2.x (13):整合ActiveMQ
ActiveMQ5.x不多做介绍了,主要是SpringBoot的整合 特点: 1)支持来自Java,C,C ++,C#,Ruby,Perl,Python,PHP的各种跨语言客户端和协议 2)支持许多高 ...
- Python定时任务sched(一)
这里介绍一下python中定时任务:sched python中自带的是sched,也可以通过pip下载schedule进行任务定时处理,这里先简单介绍下sched的使用 import datetime ...
- 1068 乌龟棋 2010年NOIP全国联赛提高组
1068 乌龟棋 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descrip ...
- Jquery ajax中表单提交被拦截的问题处理方法
在实际开发项目中,由于要做支付宝的批量退款处理,需要用到ajax中去提交表单数据,项目截图如下: 由于在第二张截图“确认退款”那里需要异步ajax提交数据到服务器处理信息,处理成功后将返回的数据装载到 ...
- JMeter配置元件作用域