• python的基础知识


    python基础
    1.dict

    a = {‘cats’:4,’dogs’:2,’pigs’:7}
    
    • 1

    获取键、值

    a.keys(), a.values()
    
    • 1

    获取所有的键值对

    a.items()
    
    • 1

    2.datetime模块

    import datetime as dt
    d1 = dt.datetime(2022,08,07)
    d2 = dt.datetime(2022,08,15)
    
    • 1
    • 2
    • 3

    #转换成字符串格式

    d1.strftime(%A’,%y%m%d’), Tuesday, 09/25/07
    
    • 1
    d = d2-d1, 返回的是一个timedelta对象,
    d.days : 8
    d.seconds: 0
    
    • 1
    • 2
    • 3

    查看今天的日期:dt.date.today() , 2022-08-07

    3.time对象

    time(hour,minute,seconds)
    
    • 1

    4.datetime对象

    datetime(year, month,day, hr, min, second, us)来创建一个datetime对象
    d1 = dt.datetime().now()  # 2022-08-07 20:58:42 148000
    时间加减
    d2 = d1 + dt.timedelta(30)
     # 2022-09-07 20:58:42 148000
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过制定格式的字符串来创建时间

    dt.datetime.strptime(2022-08-07,%m%y%d’)
    
    • 1

    #2022-08-07 00:00:00
    参考:https://ailearning.apachecn.org/#/docs/da/068

    5.函数进阶:参数传递、高阶函数、lambda匿名函数、gblobal变量、递归
    函数也是一个对象,可以将函数座位参数传递给另一个函数
    将函数作为字典存储
    将函数作为返回值

    def square(x):
    	return x*x
    
    def cube(x):
    	return x*x*x
    
    • 1
    • 2
    • 3
    • 4
    • 5

    作为字典的值

    funcs = {’square’:square,’cube’:cube}
    x = 2
    print(square(x))
    print(cube(x))
    
    for func in sorted(funds):
    	print(func, funcs[func](x)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    #可以作为回掉函数传第给另一个函数, 这样做的好处是,满足一个函数数据流的原子性

    def funk(x):
    	return x+1
    def fun(x):
    	return x-1
    def func(x, func):
    	if x>10:
    		调用函数a
    	if x<=10:
    		调用函数b
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    6.高阶函数

    map(f,sq) #将f作用到sq的每一个值上, 相当于[f(s) for s in sq]
    list(map(square,range(5))) #等价于  [square(x) for x in range(5)]
    
    • 1
    • 2

    输出 [0, 1, 4, 9, 16]
    过滤函数filter

    def is_even(x)
    	return x%2==0:
    
    [s for s in range(5) if is_even(s)]等价于
    filter(is_even,range(5))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出:0,2,4
    一起使用 map(square, filter(is_even,range(5))), 输出:[0,4,16]

    reduce函数,传入一个二元函数f(x,y), 并对于序列sq, 每次合并两个元素

    def my_add(x,y):
    	return x+y
    sq = [1,2,3,4,5]
    reduce(my_add, sq)
    
    • 1
    • 2
    • 3
    • 4

    输出:15

    reduce,顾名思义,减少的意思,通过对sq中的元素做某一操作,从而缩减其规模

    匿名函数 lambda

    s1  =map(lambda x:x*x, range(5))
    s2 = reduce(lambda x,y:x+y, map(lambda x:x**2,range(1,10))
    
    • 1
    • 2

    7.全局变量

    x = 15
    def print_x():
    	global x
    	x = 18
    	print(x)
    
    print_x() # 18
    print(x) #18, 全局变量在print_x函数中被修改
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    x = 15
    def print_x():
    	x = 18
    	print(x)
    
    print_x() # 18
    print(x) #15, 全局变量在print_x函数中未被修改
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8.生成器

    def collatz(n):
    	while n != 1:
    		if n%2 == 0:
    			n /= 2
    		else:
    			n = 3*n+1
    		yield n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    #每次print一个数,由for循环触发下一个数

    for x in collate(7):
    	print(x)
    
    • 1
    • 2

    二叉树中序遍历非递归生成器版本

    def inorder(root):
    	stack = []
    	stack.append(root)
    	#ans = []
    	while let(stack) != 0 and root is not None:
    		while root is not None:
    			root = root
    			stack.append(root)
    		root = stack.pop()
    		#ans.append(root.val) 
    		yield root.val
    		root = root.right
    	#return ans
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    构建树

    tree = BinaryTree(
        left=BinaryTree(
            left=BinaryTree(1),
            value=2,
            right=BinaryTree(
                left=BinaryTree(3),
                value=4,
                right=BinaryTree(5)
            ),
        ),
        value=6,
        right=BinaryTree(
            value=7,
            right=BinaryTree(8)
        )
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    调用

    for value in Tree:
    	print value
    
    • 1
    • 2
  • 相关阅读:
    用Python在XML和Excel表格之间实现互转
    找的笔试题的复盘(一)
    OSPF协议LSDB同步过程和邻居状态机
    c++11 explicit range
    工业信息物理系统攻击检测增强模型
    云服务器Centos8.2SSH登陆、安装部署图形化界面以及VNC连接一文详解
    springcloud高频面试题
    vue3父组件提交校验多个子组件
    elasticSearch 接口实现查询热词统计
    LeetCode 0670. 最大交换
  • 原文地址:https://blog.csdn.net/CCSUXWZ/article/details/126214720