Zhong__Python实例很多条

时间:2021.05.27

环境:Windows

目的:Python实例若干条

说明:

作者:Zhong QQ交流群:121160124 欢迎加入!

目录

列表推导式

数组去重

运行计时器

base64编解码

字典推导式

回调

map结合lambda获取字段内容

指定位数字符串全排列


列表推导式

print([i for i in range(1, 10)])

数组去重

ls = ['a', 1, 2, 1, 'a']
print(list(set(ls)))

运行计时器

base64编解码

import base64

test_str = "Hello"

# 编码
encode_str = base64.encodebytes(test_str.encode())  # b'SGVsbG8=\n'
print(encode_str)
print(encode_str.decode())  # 默认以utf8解码

# 解码
decode_str = base64.decodebytes(encode_str)
print(decode_str)  # b'Hello'
print(decode_str.decode())  # 默认以utf8解码,结果 Hello

字典推导式

annotate = [{"category_name":"科技", "count": 1}, {"category_name":"农业", "count": 2}]
_dict = {i["category_name"]:i["count"] for i in annotate}
print(_dict)

回调

class T1():

    def t1(self, func):
        print(1)
        self.callback = func()
        print(2)

    def callback(self):
        print("回调")

def func():
    print('he')

t1 = T1()
t1.t1(func)

map结合lambda获取字段内容

profile = {"name":"a", "age":18}
name, age, gender = map(lambda x: profile.get(x, None), ("name", "age", "gender"))
print(name)
print(age)
print(gender)

指定位数字符串全排列

A = 'ABCDEF'


# 方法一:
from itertools import product

print(list(product(A, repeat=2)))

# 方法二:
print([x + y for x in A for y in A])


# 方法三
def pro(strings, repeat=2):
    pools = [strings] * repeat
    result = [""]
    for pool in pools:
        ls = []
        for x in result:
            for y in pool:
                ls.append(x + y)
        # result = [x + y for x in result for y in pool]
        result = ls

    print(result)


pro(A, 2)

关注微信公众号  如果对你有帮助请多多支持

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我变了_我没变

随意 。。。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值