原创 python生成器函數的使用(模擬cycle函數)

包含yield的語句的函數可作爲生成器對象用next(iterator)或__next__()調用(兩個下劃線),yield代表此處返回一個值並暫停後面語句 def f(it): while True: f

原创 Python字符串常用方法(split,partition,maketrans,strip...)

x = "apple peach watermelon pineapple" print("原字符串: ", x) print("split(' '),以空格分隔字符串(從左至右): ", x.split(" ")) print(

原创 Python的filter、map、reduce與lambda結合使用

from _functools import reduce x = [3, 5, 6, 8, 9, 4, 12, 27] print("filter過濾能被3模除的數:", list(filter(lambda x:x % 3 =

原创 Python計算數組的n位全排列(permutations的使用)

計算[1,2,3,4]的3位全排列 from itertools import permutations for i in permutations([1, 2, 3, 4], 3): print(''.join(map(

原创 Python使用正則表達式

findall方法有返回值爲匹配成功,返回值爲[]爲匹配失敗 import re pattern = '^[0-9]*$' print(pattern, "匹配數字", re.findall(pattern, '12345'))

原创 python的簡單GUI(登錄窗口)

用到了tkinter包。 1.勾選記憶後用戶名和密碼會以文件的形式保存在本地磁盤 2.輸入錯誤三次後關閉 3.密碼長度應大於6 import tkinter import tkinter.messagebox import os

原创 Python循環遍歷(cycle)

from itertools import cycle x = cycle('Hello World!') for i in range(15): print(next(x), end=" ")

原创 Python計算從n個元素中任選i個的組合數C(n,i)

已知計算組合數C(n,i)公式,所以用reduce寫: from _functools import reduce def Cni(n, i): return reduce(lambda x, y:x * y, rang

原创 python的實例類方法、修飾器類方法、修飾器保護方法、修飾器靜態方法中私有屬性的區別和自定義property的讀寫方法

class Cla: __count=0 def __init__(self,v): self.__value=v Cla.__count+=1 def show(self)

原创 python的父類和子類的繼承關係和super()的使用

子類繼承父類的私有方法重寫無效,公有方法可以重寫,引用父類方法可用super()實現 class A(object): def __init__(self): self.__private()

原创 Maven工程Spring框架AOP的簡單使用

目錄結構: 引入的jar包 aspect.Validate.java package aspect; import java.text.*; import java.util.*; public class Validat

原创 Python6行代碼解決爬樓問題(假設一段樓梯共n個臺階,小明一步最多能上3個臺階,那麼小明上這段樓梯一共有多少種方法?)

問題描述: 假設一段樓梯共n個臺階,小明一步最多能上3個臺階,那麼小明上這段樓梯一共有多少種方法? 上第一個臺階,方法:1種(1) 上第二個臺階,方法:2種(11,2) 上第三個臺階,方法:4種(111,12,21,3) 所以共有

原创 Python的permutations和combinations的區別

permutations(計算列表長度爲2的全排序,長度默認爲迭代參數的長度): from itertools import permutations for aggregate in permutations(range(1,7

原创 Python用正則表達式匹配ABAC和AABB的詞語

正則表達式前加r是爲了不轉義反斜槓後的字符,\3代表此處重複出現子模式3 from re import findall text = '''行屍走肉、金蟬脫殼、百裏挑一、金玉滿堂、 背水一戰、霸王別姬、天上人間、不吐不快、海闊天空

原创 Python一行代碼求一年第幾天問題

import datetime print("today:",datetime.date.today()) print("Today is the",datetime.date.today().timetuple().tm_yda