python查缺补漏--持续更新

175 阅读4分钟

and 和 or 没有其他限定条件下 and的优先级优于or (not > and > or)

  1. and:x and y 的结果只能是x或者y, x为true时结果时y, x为false时结果是x

    >>> 1 and 2
    2
    >>> 0 and 2
    0
    
  2. or:x or y 的结果只能是x或者y,x为true时结果是x, x为false时结果时y

    >>> 1 or 2
    1
    >>> 0 or 2
    2
    

divmod(a,b) 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)

divmod(1, 2)
divmod(4, 2)
--------------
(0, 1)
(2, 0)

callable() 函数用于检查一个对象是否是可调用的。如果返回 True,object 仍然可能调用失败;但如果返回 False,调用对象 object 绝对不会成功。

def test(n):
    return n

a = lambda x:x**2
b = (1,)
callable(test)
callable(a)
callable(b)
------------------------------
True
True
False

整理字符串输入

user_input = "This\nstring has\tsome whitespaces...\r\n"
character_map = {ord('\n'): ' ',    ord('\t'): ' ',    ord('\r'): None}
user_input.translate(character_map)
-------------------------------------------
"This string has some whitespaces... "

跳过可迭代对象开头

string_from_file = """// Author: ...
// License: ...
//// Date: ...Actual content...
尼玛"""
import itertools
for line in itertools.dropwhile(lambda line: line.startswith("//"), string_from_file.split("\n")):    
    print(line)
----------------------------------------
尼玛

通过一个关键字给字典排序(from operator import itemgetter)

from operator import itemgetter

rows = [ {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
 {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
 {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}, 
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004} ]

a = sorted(rows, key=itemgetter('uid'))
# a = sorted(rows, key=itemgetter('uid','fname'))
--------------------------------------------
[
    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}, {'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]

自定义可使用with的语句 contextmanager

from contextlib import contextmanager

@contextmanager
def tag(name):
    print(f"<{name}>")
    yield
    print(f"</{name}>")


with tag("h1"):
    print("This is Title.")
---------------------------------
>>> <h1>
>>> This is Title.
>>> </h1>

python 命令行库 click

  • 任意嵌套命令
  • 自动生成帮助
  • 支持运行时延迟加载子命令
  • 参数介绍: --count 自定义的参数 default 指定默认类型 prompt 未传参数时的提示语 help 指定帮助信息
# 写在test.py文件中
@click.command()
@click.option('--count', default=1, help='循环次数')
@click.option('--name', prompt='你的名字', help='要循环的名字')
def hello(count, name):
    for i in range(count):
        click.echo(f"hello:{name}")

if __name__ == "__main__":
    hello()
--------------------------------------------
>>> python test.py --count 1 --name handsome
>>> hello:handsome

中文分词

def cut_by_ngram(word, min_n, max_n):
    """ 中文分词 """
    rst = []
    # 遍历切分长度的范围
    for length in range(min_n, min(len(word), max_n) + 1):
        # 按照此时切分长度进行切分
        for idx in range(0, len(word) - length + 1):
            rst.append(word[idx: idx + length])
    return rst

动态删除可变序列 用列表表达式或其他表达式

lst = [1, 2, 3, 6, 5, 4]
# 删除3的倍数
for i, e in enumerate(lst):
    if e % 3 == 0:
        lst.remove(e)
print(lst)
--------------------
[1,2,6,5,4]

[i for i in lst if i % 3]
----------------
[1,2,5,4]

闭包与lambda函数

def bibao():
    return [lambda x:i*x for i in range(5)]
# 因为闭包是迟绑定 用到的变量只有在函数用到的时候才会得到
for i in bibao():
    print(i(2))
-------------------
8
8
8
8
8

# 解决方法 闭包变成局部变量
def jubu():
    return [lambda x,i=i:i*x for i in range(5)]

for i in jubu():
    print(i(2))
-------------------------------------
0
2
4
6
8

python字典中 1 与 1.0 当作键是相同的

c = {}
c[1] = 1
c['1'] = 2
c[1.0] = 3
print(c)
---------------------
{1:3, '1':2}

translate() 方法

# str.translate(table)
# bytes.translate(table[, delete])    
# bytearray.translate(table[, delete]) 

# 使用哈希值替换掉一个字符\n \t
user_input = "This\nstring has\tsome whitespaces...\r\n"
character_map = {ord('\n'): ' ',    ord('\t'): ' ',    ord('\r'): None}
user_input.translate(character_map)

s = '你个和密码'
s.translate(str.maketrans('密码','**'))
>>> '你个和**'

b = b'123456'
b.translate(bytes.maketrans(b'56',b'$$'), b'4')
>>> b'123$$'

字符串方法 partition(mystr) 用mystr把字符串拆分为三部分 rpartition(mystr)从右边开始

test = 'hello world! bbb'
test.partition('!')
>>> ('hello world', '!', ' bbb')
test = 'hello world! bbb'
test.rpartition('b')
>>> ('hello world! bb', 'b', '')

is_integer() 方法用来判断一个float类型的数是否为整数

a= 1.0
b=1.213
a.is_integer()
>>> True
b.is_integer()
>>> False

更新字典的方法

# 法1
dict(dic1, **dic2)
# 法2
dic1.update(dic2)

字典的setdefault方法 dict.setdefault(key, [default])

有值返回值,没有值返回默认值
>>> a = {"a":1, "b":2}
>>> need = a.setdefault('a', [])
>>> need
1
>>> need = a.setdefault('c', [])
>>> need
[]

itemgetter根据元组的某个字段给元 组列表排序

>>> metro_data = [('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))]

>>> from operator import itemgetter
>>> for city in sorted(metro_data, key=itemgetter(1)):
...     print(city)
...
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889))
('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
('Mexico City', 'MX', 20.142, (19.433333, -99.133333))
('New York-Newark', 'US', 20.104, (40.808611, -74.020386))

# 示例2
>>> cc_name = itemgetter(1,2)
>>> for city in metro_data:
...     print(cc_name(city))
...
('JP', 36.933)
('IN', 21.935)
('MX', 20.142)
('US', 20.104)
('BR', 19.649)
>>>

attrgetter 获取命名元组中属性的值

>>> from collections import namedtuple
>>> LatLong = namedtuple('LatLong', 'lat long')
>>> Metropolis = namedtuple('Metropolis', 'name cc pop coord')
>>> metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long)) for name,cc,pop,(lat,long) in metro_data]

>>> metro_areas
[Metropolis(name='Tokyo', cc='JP', pop=36.933, coord=LatLong(lat=35.689722, long=139.691667)), Metropolis(name='Delhi NCR', cc='IN', pop=21.935, coord=LatLong(lat=28.613889, long=77.208889)), Metropolis(name='Mexico City', cc='MX', pop=20.142, coord=LatLong(lat=19.433333, long=-99.133333)), Metropolis(name='New York-Newark', cc='US', pop=20.104, coord=LatLong(lat=40.808611, long=-74.020386)), Metropolis(name='Sao Paulo', cc='BR', pop=19.649, coord=LatLong(lat=-23.547778, long=-46.635833))]

>>> from operator import attrgetter
>>> name_lat = attrgetter('name','coord.lat')
>>> for city in sorted(metro_areas, key=attrgetter('coord.lat')):
...     print(name_lat(city))
...
('Sao Paulo', -23.547778)
('Mexico City', 19.433333)
('Delhi NCR', 28.613889)
('Tokyo', 35.689722)
('New York-Newark', 40.808611)

methodcaller 自行创建函数

>>> from operator import methodcaller
>>> s = 'winter is comming'
>>> upcase = methodcaller('upper')
>>> upcase(s)
'WINTER IS COMMING'
>>> tihuan = methodcaller('replace', ' ', '-')
>>> tihuan(s)
'winter-is-comming'
>>>

functools.partial 这个高阶函数用于部分应用一个函数。部分应用 是指,基于一个函数创建一个新的可调用对象,把原函数的某些参数固定

>>> from operator import mul
>>> mul(4, 5)
20
>>> from functools import partial
>>> triple = partial(mul, 3)
>>> triple(7)
21