• [技术干货] python利用format方法生成一个星号三角形
    python编程实现,从控制台输入n,利用format方法生成一个星号三角形vi demo16.py'''从控制台输入n,利用format方法生成一个星号三角形    *   ***  ***** *******'''floorStr = input('请输入一个层数:')floor = int(floorStr)num = floor * 2 -3   # 17while floor > 0:    print("{:<{a}}{:*<{b}}".format(" ","",a =floor,b=(num - (floor - 2)*2)))    floor -= 1运行结果:[root@ecs-0720 ~]# python demo16.py请输入一个层数:10          *         ***        *****       *******      *********     ***********    *************   ***************  ***************** *******************[root@ecs-0720 ~]# 
  • [技术干货] python执行linux系统命令
    在python中执行linux的命令,一般有四种方式:1.os.system方法。[root@ecs-0720 ~]# pythonPython 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import os>>> os.system('date')Thu Oct  7 21:43:03 CST 202102.os.popen方法。>>> nowtime=os.popen('date')>>> print (nowtime.read())Thu Oct  7 21:44:07 CST 20213.commands模块>>> import commands>>> a,b=commands.getstatusoutput('date')>>> print(a)0>>> print(b)Thu Oct  7 21:47:42 CST 20214. subprocess 模块>>> import subprocess>>> nowtime = subprocess.Popen('date', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)>>> print(nowtime.stdout.read())Thu Oct  7 21:50:01 CST 2021>>> 
  • [其他] 介绍用于图像识别的五大最佳编程语言!
    1.Python 目前,Python被认为一种目前最流行的编程语言。其简单性和多功能性是许多程序员喜欢使用它的一些原因。尽管很简单,但Python是一种可以依赖于执行复杂任务的语言。它可用于创建图像处理和识别功能。Python的普及使其资源非常丰富。它包含几个功能所需的库,其中包括图像识别。最强大和最有效的库之一是Scikit-Learn机器学习库。除了图像识别,这些库还可用于更智能的功能,如人脸识别和运动检测。2. Matlab Matlab是一种独立的编程语言,它有自己的框架和集成开发环境(IDE),具有更强大的工作空间。该编程语言提供了广泛的工具,你可以使用它们轻松处理更多技术编程任务。图像识别和面部处理是Matlab可以处理的一些任务。Matlab提供了一系列用于矩阵计算的内置工具。必须注意的是,图像识别和矩阵计算是齐头并进的。Matlab中可用的一些工具可以执行复杂的图像处理任务,例如裁剪、旋转、掩蔽等。还有一个专用的Matlab代码,用于调用和实现面部识别功能。这个代码使用AT&T数据库。因此,你应该在使用这个代码之前下载AT&T数据库。最先进的人脸识别应用程序是用Matlab编写的。3. C / C ++ / C# 在C系列编程语言方面,你永远不会出错。它们功能强大,可以做任何事情,包括创建图像处理和识别功能。C系列编程语言提供了两种创建图像处理功能的选项。你可以选择从头开始对所有代码进行编码,从而手动编写代码。第二种选择是使用专为这些编程语言设计的现有库。这些库包括OpenGL、EmguCV、OpenCV等等。它们具有用于图像识别的智能图像处理功能。4. Java像C和C ++一样,永远不要低估Java编程语言的强大功能。这种语言足以执行复杂的功能。它可用于创建图像处理和图像识别的应用程序。5. OpenCV工具开源计算机视觉(OpenCV)是一种用于实时处理的高级工具。它也是一种跨平台工具,因此可以集成到任何编程语言中,以执行图像处理和识别功能。它可以与C、C ++、Java、Python和Android编程语言集成。OpenCV提供无专利算法,你可以在没有任何法律限制的情况下使用。它可用于商业和学术目的。它有一个专门的脸部识别器类,你可以用它来试验图像识别功能的能力,没有任何麻烦。该课程附有一个信息丰富的文档,将向你展示如何实现图像识别功能。总体而言,无论使用何种编程语言,OpenCV都是图像识别的首选工具。
  • [技术干货] python数学函数应用
    python中的math模块,用来实现数学计算算法。[root@ecs-0720 ~]# vi ch16.py# -*- coding: utf-8 -*-import mathimport sysprint(sys.getdefaultencoding())print('圆周率=%03.23f'%math.pi)print('自然常数=%02.6f'%math.e)# 取绝对值print(math.fabs(-1.2))# 向上取整print(math.ceil(1.3))# 向下取整print(math.floor(1.8))#返回 xy(x的y次方) 的值。print(math.pow(2,10))# 返回开平方根的值。print(math.sqrt(8))print(math.sin(math.pi/2))print(math.cos(math.pi))print(math.tan(math.pi/4))执行结果:[root@ecs-0720 ~]# python ch16.pyascii圆周率=3.14159265358979311599796自然常数=2.7182821.22.01.01024.02.828427124751.0-1.01.0[root@ecs-0720 ~]# 
  • [技术干货] python 随机数应用
    python随机数的应用,使用random模块。应用示例:vi ch15.py# -*- coding: utf-8 -*-import random# 产生1到100之间的随机整数print(random.randint(1,100))# 产生0到1之间的随机数print(random.random())# 从[1,20,3]print(random.randrange(1,20,3))# 产生一个从1到100.5的随机浮点数print(random.uniform(1,100.5))intList = [1,2,5,7,76,10,4,8]print(random.choice(intList))newList = random.sample(intList,3)print(newList)random.shuffle(intList)print(intList)执行结果:[root@ecs-0720 ~]# python ch15.py970.3637611966831087.07609958251[4, 5, 8][2, 8, 76, 4, 10, 7, 5, 1][root@ecs-0720 ~]# 
  • [技术干货] python 获取指定日期的日历
    python calendar模块和locale模块可以用来获取指定日期的日历。locale分类:locale把按照所涉及到的文化传统的各个方面分成12个大类,这12个大类分别是:1、语言符号及其分类(LC_CTYPE) 2、数字(LC_NUMERIC) 3、比较和排序习惯(LC_COLLATE) 4、时间显示格式(LC_TIME) 5、货币单位(LC_MONETARY) 6、信息主要是提示信息,错误信息,状态信息,标题,标签,按钮和菜单等(LC_MESSAGES) 7、姓名书写方式(LC_NAME) 8、地址书写方式(LC_ADDRESS) 9、电话号码书写方式(LC_TELEPHONE) 10、度量衡表达方式 (LC_MEASUREMENT) 11、默认纸张尺寸大小(LC_PAPER) 12、对locale自身包含信息的概述(LC_IDENTIFICATION)。所以说,locale就是某一个地域内的人们的语言习惯和文化传统和生活习惯。locale.setlocale(locale.LC_ALL, '') 相当于默认采用系统locale设置,没有做任何改变。vi ch14.pyimport calendarimport locale# 返回2021年10月份日历cal = calendar.month(2021,10)print(cal)locale.setlocale(locale.LC_ALL, '')print(calendar.calendar(2021))运行结果:[root@ecs-0720 ~]# python ch14.py    October 2021Mo Tu We Th Fr Sa Su                     1   2   3 4   5   6   7   8   9  1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31                                  2021      January                   February                   MarchMo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su             1  2  3       1  2  3  4  5  6  7       1  2  3  4  5  6  7 4  5  6  7  8  9 10       8  9 10 11 12 13 14       8  9 10 11 12 13 1411 12 13 14 15 16 17      15 16 17 18 19 20 21      15 16 17 18 19 20 2118 19 20 21 22 23 24      22 23 24 25 26 27 28      22 23 24 25 26 27 2825 26 27 28 29 30 31                                29 30 31       April                      May                       JuneMo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su          1  2  3  4                      1  2          1  2  3  4  5  6 5  6  7  8  9 10 11       3  4  5  6  7  8  9       7  8  9 10 11 12 1312 13 14 15 16 17 18      10 11 12 13 14 15 16      14 15 16 17 18 19 2019 20 21 22 23 24 25      17 18 19 20 21 22 23      21 22 23 24 25 26 2726 27 28 29 30            24 25 26 27 28 29 30      28 29 30                          31        July                     August                  SeptemberMo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su          1  2  3  4                         1             1  2  3  4  5 5  6  7  8  9 10 11       2  3  4  5  6  7  8       6  7  8  9 10 11 1212 13 14 15 16 17 18       9 10 11 12 13 14 15      13 14 15 16 17 18 1919 20 21 22 23 24 25      16 17 18 19 20 21 22      20 21 22 23 24 25 2626 27 28 29 30 31         23 24 25 26 27 28 29      27 28 29 30                          30 31      October                   November                  DecemberMo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su             1  2  3       1  2  3  4  5  6  7             1  2  3  4  5 4  5  6  7  8  9 10       8  9 10 11 12 13 14       6  7  8  9 10 11 1211 12 13 14 15 16 17      15 16 17 18 19 20 21      13 14 15 16 17 18 1918 19 20 21 22 23 24      22 23 24 25 26 27 28      20 21 22 23 24 25 2625 26 27 28 29 30 31      29 30                     27 28 29 30 31linux屏幕上显示还是相当规整的。
  • [技术干货] python 计算日期和时间的差值
    python使用datetime模块中的timedelta函数构造时间或日期的差值,进而构造出想要的日期和时间。# -*- coding: utf-8 -*-import datetimed1 = datetime.datetime(2019,3,22)d2 = datetime.datetime(2019,3,25)print((d2-d1).days)d1 = datetime.datetime(2019,3,12,1,1,1)d2 = datetime.datetime(2019,3,12,1,1,4)print(d2 - d1)print((d2 - d1).seconds)d1 = datetime.datetime.now()d2 = d1 + datetime.timedelta(days=10)print(d2)d2 = d1 + datetime.timedelta(hours=-10)print(d2)运行结果:[root@ecs-0720 ~]# python c13.py30:00:0332021-10-09 14:09:05.8350482021-09-29 04:09:05.835048[root@ecs-0720 ~]# 
  • [技术干货] python 时间元组
    联系python时间元组的应用:做本次实验中遇到一个错误,新建了一个time.pyvi time.py# -*- coding: utf-8 -*-import time# 获取当前时间localtime = time.localtime()print(localtime)  # localtime是一个对象print(type(localtime))  st = time.struct_time((1,2,3,4,5,6,7,8,9))print(st)print('年','=',localtime.tm_year)print('月','=',localtime.tm_mon)print('日','=',localtime.tm_mday)print('一年的第%d天' % localtime[7])# 获取一个刻度的时间localtime = time.asctime(localtime)print(localtime)运行时报错,意识到time.py原文件会对time函数干扰,直接把time.py重命名为t.py。运行时依然报错:[root@k8s-master-c71e ~]# python t.py    Traceback (most recent call last):  File "t.py", line 2, in <module>    import time  File "/root/time.py", line 15, in <module>AttributeError: 'module' object has no attribute 'localtime'这里说的时模块time中没有对象的属性是localtime,上一行的信息就很明显了,import time首先在当前目录找time.py,咦,我已经删掉time.py了啊。ls,发现还有一个time.pyc文件。删掉,再次运行:[root@k8s-master-c71e ~]# rm time.pycrm: remove regular file ‘time.pyc’? y[root@k8s-master-c71e ~]# python t.py     time.struct_time(tm_year=2021, tm_mon=9, tm_mday=28, tm_hour=14, tm_min=26, tm_sec=25, tm_wday=1, tm_yday=271, tm_isdst=0)<type 'time.struct_time'>time.struct_time(tm_year=1, tm_mon=2, tm_mday=3, tm_hour=4, tm_min=5, tm_sec=6, tm_wday=7, tm_yday=8, tm_isdst=9)('\xe5\xb9\xb4', '=', 2021)('\xe6\x9c\x88', '=', 9)('\xe6\x97\xa5', '=', 28)一年的第271天Tue Sep 28 14:26:25 2021[root@k8s-master-c71e ~]# 中文提示部分,很有趣!,有显示正常的,也有不正常的。why?
  • [技术干货] python双端队列
    python中双端队列deque,可以从头尾两端操作。应用示例:vi dd.py# -*- coding: utf-8 -*-from collections import deque# 将列表转换为双端队列q = deque(range(10))print(q)# 将123追加到双端队列q的队尾q.append(123)q.append(-32)print(q)# 将30添加到q的队首q.appendleft(30)print(q)# 弹出队尾的值print(q.pop())# 弹出队首的值print(q.popleft())print(q)# 将q向左循环移动2个位置q.rotate(-2)print(q)#将q向右循环移动4个位置q.rotate(4)print(q)#创建一个双端队列q1q1=deque(['a','b'])q.extend(q1)print(q)q.extendleft(q1)print(q)[root@k8s-master-c71e ~]# python dd.pydeque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 123, -32])deque([30, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 123, -32])-3230deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 123])deque([2, 3, 4, 5, 6, 7, 8, 9, 123, 0, 1])deque([9, 123, 0, 1, 2, 3, 4, 5, 6, 7, 8])deque([9, 123, 0, 1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b'])deque(['b', 'a', 9, 123, 0, 1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b'])[root@k8s-master-c71e ~]# 值得注意:extendleft()这里会从左侧倒序插入。
  • [技术干货] python 堆的应用
    堆是python中另外一种数据结构,它是优先队列的一种,python中没有独立的堆类型,但有一个堆操作的函数模块。heap = [] #建立一个常见的堆heappush(heap,item) #往堆中插入一条新的值heappop(heap) #弹出堆中最小的值iheap[0] #查看堆中最小的值,不弹出heapify(x) #以线性时间将一个列表转为堆heapreplace(heap,item) #弹出一个最小的值,然后将item插入到堆当中。堆的整体的结构不发生改变。heappoppush() #弹出最小的值,并且将新的值插入其中merge() #将多个堆进行合并nlargest(n , iterbale, key=None) #从堆中找出做大的N个数,用列表元素的某个属性和函数作为关键字。应用示例:[root@k8s-master-c71e ~]# vi heap.py    # -*- coding: utf-8 -*-from heapq import *from random import *data = [1,2,3,4,5,6,7,8,9]heap = []for n in data:    value = choice(data)    # 将值添加到堆    heappush(heap,value)print(heap)heappush(heap,30)heappush(heap,45)print(heap)print(heappop(heap)) #弹出堆中最小的值data1 = [6,3,1,12,9] #新定义一个列表heapify(data1) #将列表转化成堆print(data1)print(heapreplace(data1, 123)) #将堆中的最小值替换为123print(data1)print(nlargest(1,data1)) #从堆中找出做大的1个数print(nlargest(2,data1)) #从堆中找出做大的2个数print(nsmallest(1,data1)) print(nsmallest(2,data1))print(nsmallest(3,data1))print(list(merge([1,3,456,7],[0,1,-5,6],[1,7,4],[],[67])))print(list(merge(['dog','horse'],['cat','fish','kangaroo'])))"heap.py" 32L, 621C written[root@k8s-master-c71e ~]# python heap.py[1, 2, 1, 2, 9, 8, 3, 7, 4][1, 2, 1, 2, 9, 8, 3, 7, 4, 30, 45]1[1, 3, 6, 12, 9]1[3, 9, 6, 12, 123][123][123, 12][3][3, 6][3, 6, 9][0, 1, 1, -5, 1, 3, 6, 7, 4, 67, 456, 7]['cat', 'dog', 'fish', 'horse', 'kangaroo'][root@k8s-master-c71e ~]# 
  • [技术干货] Python frozenset() 函数使用
    python将集合做为集合的一个元素,需要使用 frozenset() 函数。应用示例:[root@k8s-master-c71e ~]# vi fset.py    a = set([1,2])b = set([10,20])a.add(4)print(a)#a.add(b) 直接加一个集合做集合的元素会报错。a.add(frozenset(b))print(a)d = {'Bill':30,'John':34}#d[a] = 10 报错d[frozenset(a)] = 10print(d)#b.add(d) 报错t = (1,2,3,4)b.add(t)d[t] = 10     print(d)                                                                                                                                                                                                                                    ~                                                                                                                                                                                         ~                                                                                                                            "fset.py" [New] 18L, 202C written由于集合的元素和字典中的key都不允许是可变的值,所以集合、列表和字典,不能直接作为集合的元素。但元组是静态元素,所以元组既可以做集合的元素,也可以做字典的key运行结果:[root@k8s-master-c71e ~]# python fset.pyset([1, 2, 4])set([frozenset([10, 20]), 1, 2, 4]){'Bill': 30, 'John': 34, frozenset([frozenset([10, 20]), 1, 2, 4]): 10}{'Bill': 30, 'John': 34, (1, 2, 3, 4): 10, frozenset([frozenset([10, 20]), 1, 2, 4]): 10}[root@k8s-master-c71e ~]# 
  • [技术干货] python集合的使用
    集合,必须满足以下3个条件1. 无序性:集合中每一个元素的值都是平等的,元素之间是无序的。2. 互异性:集合中任意两个元素都是不同的,每个元素只能在集合中出现一次3. 确定性:集合中每个元素都是确定的,对于一个值来说,要么属于该集合,要么不属于该集合。所以集合、列表、字典都不能作为集合的元素值,因为他们都是可变的。集合的操作创建集合、合并集合、集合相交、集合的差等[root@k8s-master-c71e ~]# vi set.py # -*- coding: utf-8 -*-set1 = set(range(10))print(type(set1))set2 = set('helloabc')print(set2)print(set1)set3 = set(['Pill','John','Mike','John'])print(set3)a = set((1,2,3))b = set([3,5,1,6])# 集合的合并print("set a:",a)print("set b:",b)print("set a&b:",a.union(b))print("set a&b:",a | b)# 求a和b的交集print("set a*b:",a.intersection(b))print("set a*b:",a & b)运行结果:[root@k8s-master-c71e ~]# python set1.py<type 'set'>set(['a', 'c', 'b', 'e', 'h', 'l', 'o'])set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])set(['Mike', 'John', 'Pill'])('set a:', set([1, 2, 3]))('set b:', set([1, 3, 5, 6]))('set a&b:', set([1, 2, 3, 5, 6]))('set a&b:', set([1, 2, 3, 5, 6]))('set a*b:', set([1, 3]))('set a*b:', set([1, 3]))子集和超集的判断:vi set.py # -*- coding: utf-8 -*-a = set((1,2,3))b = set([3,5,1,6])c = set([2,3])print(c.issubset(a))print(a.issubset(c))# 超集print(c.issuperset(a))[root@k8s-master-c71e ~]# python set.py TrueFalseFalse
  • [技术干货] python的目录操作2
    在当前目录下,如果不存在newdir1,则生成newdir1的目录:# -*- coding: utf-8 -*-import osif not os.path.exists('newdir1'):    os.mkdir('newdir1')#os.mkdir('a/b/c')# 创建多级目录os.makedirs('x/y/z',0o733)之后删除单个目录newdir1,生成mydir之后将mydir改名为yourdir。将多级目录./x/y/x改名为./bill/mike/johntry:    # 删除单个目录    os.rmdir('newdir1')except OSError as e:    print(e)if not os.path.exists('mydir'):    os.mkdir('mydir')    os.rename('mydir','yourdir')if os.path.exists('x/y/z'):    # 重命名多级目录    os.renames('x/y/z','bill/mike/john')测试是否存在test.txt在youdir目录下,如果存在则删除。if os.path.exists('youdir/test.txt'):    os.remove('youdir/test.txt')
  • [技术干货] python的目录操作1
    调用os模块,使用os.getcwd()获取当前目录下的文件列表,运行结果表明:相当于运行了ls -a。然后改变当前目录为根目录。再次运行os.getcwd()获取当前目录下的文件列表。vi end.py# -*- coding: utf-8 -*-import os#获取当前的工作目录print('pwd:',os.getcwd())print('---------------------------------------------------------------')print(os.listdir(os.getcwd()))print('---------------------------------------------------------------')os.chdir('../')print('changed pwd:',os.getcwd())print(os.listdir(os.getcwd()))[root@k8s-master-c71e ~]# python end.py('pwd:', '/root')---------------------------------------------------------------['.ssh', '.head.py.swm', '.end2.py.swp', 'split.py', '.bash_logout', '.history', '.bash_profile', '.tcshrc', 'get-pip.py', 'sub.py', '.bashrc', 'test', 'head.py', '.cshrc', '.head.py.swp', '.end.py.swp', 'end.py', '.tt.py.swp', 'subN.py', '.head.py.swo', '.bash_history', '.cache', '.pki']---------------------------------------------------------------('changed pwd:', '/')['etc', 'sbin', 'boot', 'tmp', 'mnt', 'proc', 'bin', 'lost+found', 'dev', 'lib', 'media', 'lib64', 'CloudrResetPwdAgent', 'home', '.autorelabel', 'ttt.py', 'usr', '.ttt.py.swp', 'var', 'srv', 'opt', 'run', 'sys', 'CloudResetPwdUpdateAgent', 'root'][root@k8s-master-c71e ~]# lsend.py  get-pip.py  head.py  split.py  subN.py  sub.py  test[root@k8s-master-c71e ~]# cd /root[root@k8s-master-c71e ~]# cd /[root@k8s-master-c71e /]# lsbin   CloudResetPwdUpdateAgent  dev  home  lib64       media  opt   root  sbin  sys  ttt.py  varboot  CloudrResetPwdAgent       etc  lib   lost+found  mnt    proc  run   srv   tmp  usr[root@k8s-master-c71e /]# cd root[root@k8s-master-c71e ~]# lsend.py  get-pip.py  head.py  split.py  subN.py  sub.py  test[root@k8s-master-c71e ~]# ls -a.   .bash_history  .bash_profile  .cache  .end2.py.swp  .end.py.swp  head.py       .head.py.swo  .history  split.py  subN.py  .tcshrc  .tt.py.swp..  .bash_logout   .bashrc        .cshrc  end.py        get-pip.py   .head.py.swm  .head.py.swp  .pki      .ssh      sub.py   test[root@k8s-master-c71e ~]# 
  • [技术干货] python常用模块sys
    在当前目录下生成一个./test目录,新建一个my.py[root@k8s-master-c71e ~]# vi ./test/my.pydef greet(name):    print('hello %s' % name)[root@k8s-master-c71e ~]#vi end.pyimport syssys.path.append('./test')import my      //将自定义的模块引入my.greet('lh')print(sys.modules['my'])print(type(sys.modules['my']))print(sys.platform)print(sys.argv[0])s = sys.stdin.read(3) //基本输入接口print(s)sys.stdout.writelines("this test is over!")sys.stderr.writelines("error")sys.exit(1)[root@k8s-master-c71e ~]# python end.py hello lh<module 'my' from './test/my.pyc'> //模块名<type 'module'> //模块类型linux2 //系统名称end.py //本应用名称aaa //输入aaa  //输出errorthis test is over![root@k8s-master-c71e ~]#有趣的是最后的输出,虽然标准输出在前面,错误输出在后面,但是运行的时候还是报错在前面,标准输出在后。
总条数:2474 到第 页
上滑加载中