Python 在问答频道中刷题积累到的小技巧(五)

x33g5p2x  于10天前 转载在 Python  
字(2.9k)|赞(0)|评价(0)|浏览(12)

  1. 所有字母(大小写)数字和下划线的列表或字串,不用记ASCII范围
list(map(chr,[*range(ord('A'),ord('Z')+1),*range(ord('a'),ord('z')+1),*range(ord('0'),ord('9')+1),ord('-')]))

''.join((map(chr,[*range(ord('A'),ord('Z')+1),*range(ord('a'),ord('z')+1),*range(ord('0'),ord('9')+1),ord('-')])))
# => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-'
  1. 口令密码是否有效:至少8位,大写字母、小写字母、数字每类至少一个
def validPin(str):
    import re
    return None!=re.match(r"^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]))\S{8,}$",str)

# 或者:
from string import ascii_lowercase as L,ascii_uppercase as U,digits as D
ValidPin = lambda s:all([len(s)>=8,any([i in L for i in s]),any([i in U for i in s]),any([i in D for i in s])])
  1. 口令密码等级:多一类字符就高一级,长度不够判定无效
def level(pin):
    import string
    dic = {0:"无效",1:"弱",2:"中低",3:"中高",4:"强"}
    lib = {1:string.ascii_lowercase,2:string.ascii_uppercase,3:string.digits,4:',.!;?<>'}
    pinlevel = 0
    for v in lib.values():
        for p in pin:
            if p in v:
                pinlevel += 1
                break
    return dic[pinlevel * len(pin)>=8] + '密码'

print(level(input()))
  1. 判断二月份天数,直接用28+闰年判别式
def MaxDayMonth(year, month):
    days = 31,28+(year%4==0 and year%100!=0 or year%400==0),31,30,31,30,31,31,30,31,30,31
    return 0 if (month<1 or month>12) else days[month-1]

def DayofYear(year, month, day):
    days = 31,28+(year%4==0 and year%100!=0 or year%400==0),31,30,31,30,31,31,30,31,30,31
    return sum(days[:month-1]) + day
  1. 继承父类属性的三种写法
class Animal:
    def __init__(self, name):
        self.name = name
        
class Cat(Animal):
    def __init__(self, name, age):
        super().__init__(name)             #第一种
        #super(Cat,self).__init__(name)    #第二种
        #Animal.__init__(self,name)        #第三种
        self.age = age
    def cry (self):
        print("喵~~~我是{},我{}岁了".format(self.name,self.age))
        
class Dog(Animal):
    def __init__(self, name, color):
        super().__init__(name)
        #super(Dog,self).__init__(name)
        #Animal.__init__(self, name)
        self.color = color
    def bark(self):
        print("汪~~~我是{},我穿着{}的外套".format(self.name,self.color))
        
cat = Cat('小花', 3)
cat.cry()
dog = Dog('小黄', '红色')
dog.bark()

附录 string库常用字串和内置方法

import string
string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.digits
'0123456789'
string.hexdigits
'0123456789abcdefABCDEF'
string.hexdigits
'0123456789abcdefABCDEF'
string.octdigits
'01234567'
string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_{|}~ \t\n\r\x0b\x0c' string.punctuation '!"#$%&\'()*+,-./:;<=>?@[\\]^_{|}~'
string.whitespace
' \t\n\r\x0b\x0c'

s = 'abcdaefgahijkalmn'
string.capwords(s,'a')
'aBcdaEfgaHijkaLmn'

s = 'hello,$a world$b'
t = string.Template(s)
t.substitute({'a':'间隔','b':'!!!'})
'hello,间隔 world!!!'

formatter = string.Formatter()
formatter.format('第{}页/共{}页', *{3,23})
'第3页/共23页'

相关阅读

Python 在问答频道中刷题积累到的小技巧(一)
Python 在问答频道中刷题积累到的小技巧(一)_Hann Yang的博客-CSDN博客

Python 在问答频道中刷题积累到的小技巧(二)
Python 在问答频道中刷题积累到的小技巧(二)_Hann Yang的博客-CSDN博客

Python 在问答频道中刷题积累到的小技巧(三)
Python 在问答频道中刷题积累到的小技巧(三)_Hann Yang的博客-CSDN博客

Python 在问答频道中刷题积累到的小技巧(四)
写代码也有本手俗手之分,而我们要善于发现妙手!_Hann Yang的博客-CSDN博客

相关文章