话不多说,代码说话:importnumpyasnpimportmathclassConv2D(object):def__init__(self,shape,output_channels,ksize=3,stride=1,method='VALID'):self.input_shape=shapeself.output_channels=output_channelsself.input_channels=shape[-1]self.batchsize=s
系统 2019-09-27 17:54:06 2097
本文实例讲述了python实现对象列表根据某个属性排序的方法。分享给大家供大家参考,具体如下:对于一个已有的pythonlist,里面的内容是一些对象,这些对象有一些相同的属性值,在一些特定的情况下,需要自己选择特定的排序,也就是根据某一个具体的属性来排序,在网上找了下资料,一般来说有两种方法,但从根本上来说,还是调用了list.sort方法来实现。下面是简单的测试代码片段:#coding:utf-8classPerson:def__init__(self
系统 2019-09-27 17:53:53 2097
进程是操作系统分配内存的最小单位Python中进程用到的库multiprocessing简单的多进程示例frommultiprocessingimportProcessfromtimeimporttime,sleepfromrandomimportrandintdefdownload_task(task_name):print(task_name+'开始下载')time=randint(1,5)sleep(time)print(task_name+f'下载
系统 2019-09-27 17:53:03 2097
0x00前言eval是Python用于执行python表达式的一个内置函数,使用eval,可以很方便的将字符串动态执行。比如下列代码:>>>eval("1+2")>>>eval("[xforxinrange(10)]")[0,1,2,3,4,5,6,7,8,9]当内存中的内置模块含有os的话,eval同样可以做到命令执行:>>>importos>>>eval("os.system('whoami')")win-20140812chj\administrat
系统 2019-09-27 17:53:02 2097
1.常用模块#连接数据库connect()函数创建一个新的数据库连接对话并返回一个新的连接实例对象PG_CONF_123={'user':'emma','port':123,'host':'192.168.1.123','password':'emma','database':'dbname'}conn=psycopg2.connect(**PG_CONF_123)#打开一个操作整个数据库的光标连接对象可以创建光标用来执行SQL语句cur=conn.cur
系统 2019-09-27 17:52:21 2097
阅读更多操作列表#列表循环for循环(for**in**)1.注意使用for循环时print前要缩进cats=["alice","clear","dell",'moon']forcatincats:print(cat)#可在for循环中执行更多操作#2.不使用for循环时,切记print能缩进cats=["alice","clear","dell",'moon']forcatincats:print(cat)print("theyaresocute")#在
系统 2019-09-27 17:51:30 2097
在写程序时,我们经常需要定义一些路径常量,为了逻辑和代码的清晰,可以新建一个config.ini文件,然后调用其中的信息即可。config.ini文件格式如下:[section1]model_dir=/home/user/nlp-data/trained-model;注释save_input=./profiling/inputsave_state=./profiling/state......[section2]save_output=./profilin
系统 2019-09-27 17:51:24 2097
学了一个多月的python,做了一个小程序:python实现简单成绩录入系统,实验一下menu部分fromtkinterimport*#这是一个python模块,python3中都有importtkinter.messagebox#这也是一个模块fromfile_readimportreadfromfile_writeimportwriteclassstudent_main():#定义一个学生类def__init__(self):self.name=''s
系统 2019-09-27 17:50:34 2097
Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义。一、函数式装饰器:装饰器本身是一个函数。1.装饰函数:被装饰对象是一个函数[1]装饰器无参数:a.被装饰对象无参数:复制代码代码如下:>>>deftest(func):def_test():print'Callthefunction%s().'%func.func_namereturnfunc()return_test>>>@testdefsay():return'hellowor
系统 2019-09-27 17:50:21 2097
python中with可以明显改进代码友好度,比如:复制代码代码如下:withopen('a.txt')asf:printf.readlines()为了我们自己的类也可以使用with,只要给这个类增加两个函数__enter__,__exit__即可:复制代码代码如下:>>>classA:def__enter__(self):print'inenter'def__exit__(self,e_t,e_v,t_b):print'inexit'>>>withA()
系统 2019-09-27 17:49:50 2097