zoukankan      html  css  js  c++  java
  • 【UNFinish】Python Day 16

    # 剩余 内置函数

    # 数据类型:int bool……
    # 数据结构:dict list tuple set str

    # l = [1,2,3,4,5]
    # l.reverse()
    # print(l) #反转 [5, 4, 3, 2, 1]
    #
    # l2 = [1,2,3,4,5]
    # ret = reversed(l2)
    # print(ret) # <list_reverseiterator object at 0x10391b550>不改变原来的列表,返回一个反向的迭代器
    #
    # l = (1,2,34,213,5612,342,43)
    # sli = slice(1,5,2)
    # print(l[sli]) # (2, 213)

    # print(format('test','<20')) # 左对齐
    # print(format('test','>20')) # 右对齐
    # print(format('test','^20'))# 居中显示

    # bytes 转换成bytes类型
    # print(bytes('你好',encoding='GBK')) # b'xc4xe3xbaxc3' Unicode转换成GBK
    # print(bytes('你好',encoding='utf-8')) # b'xe4xbdxa0xe5xa5xbd' Unicode转换成UTF-8
    # print(bytes('你好',encoding='GBK').decode('GBK')) # 你好
    # print(bytes('你好',encoding='utf-8').decode('UTF-8')) # 你好
    # gbk转换成utf-8
    # 先将GBK转换成unicode,再将Unicode转换成UTF-8

    # 网络编程 只能转二进制
    # 文件存储
    # 照片和视频 也是以二进制存储
    # html网页爬取到的也是编码

    # bytearray修改字符串的时候,可以转换成byte的方式,可以节省内存
    # b_array= bytearray('你好',encoding='UTF-8')
    # print(b_array) # bytearray(b'xe4xbdxa0xe5xa5xbd')
    # print(b_array[0]) # 228

    # 切片:字节类型 不占内存
    # 字节:换成字符串,占据内存
    # print(memoryview(bytes('你好',encoding='UTF-8'))) # <memory at 0x10401aac8>

    # 字符按照Unicode转数字
    # print(ord('a')) # 97
    # # 数字按照Unicode转字符
    # print(chr(97)) # a
    # # 只要是ascii中的内容,就打印出来,不是就转成u
    # print(ascii('好')) # 'u597d'
    # print(ascii('123')) # '123'


    # # repr带着字符类型打印
    # name = 'fangfang'
    # print('你好%s'%name) # 你好fangfang
    # print('你好%r'%name) # 你好'fangfang'
    #
    # print(repr('1')) # '1'
    # print(repr(1)) # 1


    # # ALL:可迭代内容里面有任意一个值是0,则整个为0
    # print(all(['a','','123'])) # False
    # print(all(['a','123'])) # True
    # print(all([0,'123'])) # False
    #
    # # Any:可迭代内容里面所有值是0,则整个为0
    # print(any(['','',0])) # False
    # print(any(['a','123'])) # True
    # print(any([0,'123'])) # True

    # # zip
    # l = [1,2,3]
    # l2 = ['a','b','c','d']
    # l3 = ('*','**',[1,2,3])
    # l4 = {'k1':'v1','k2':'v2'}
    # print(zip(l,l2)) # <zip object at 0x104108988> 是一个迭代器
    # for i in zip(l,l2,l3,l4):
    # print(i)
    # (1, 'a', '*', 'k1')
    # (2, 'b', '**', 'k2')

    # filter
    # def is_odd(x):
    # return x%2 == 1
    # def is_str(s): # 过滤字符串
    # return type(s) == str
    # def is_delete_empty(s): # 过滤空数据
    # return str(s) and str(s).strip()
    # ret = filter(is_delete_empty,[1,2,'hello',23,None,'world',7,' ',[],'hehe'])
    # for i in ret:
    # print(i) # 1 3 23 7
    # print([i for i in [1,2,3,23,6,7] if i%2 == 1 ]) # [1, 3, 23, 7]
    #
    # from math import sqrt
    # def function(x):
    # return sqrt(x)%1 == 0
    # ret = filter(function,range(1,101))
    # print(ret) # <filter object at 0x104102518>
    # for i in ret:
    # print(i)

    # MAP
    # ret = map(abs,[1,-4,6,-8]) #
    # print(ret) # <map object at 0x104100588>
    # for i in ret:
    # print(i) # 1 4 6 8

    # filter:执行了filter之后的结果集合 <= 执行之前的个数
    # filter只管筛选,不会改变原来的值

    # map:执行前后元素个数不变
    # 值可能发生改变

    # Sort:在原列表的基础上进行排序
    # Sorted:生成一个新列表 不改变原列表 占内存
    # l = [1,-4,6,5,-10]
    # print(sorted(l)) # [-10, -4, 1, 5, 6] 默认从小到大排序
    #
    # l = [1,-4,6,5,-10]
    # l.sort(key = abs)
    # print(l) # [1, -4, 5, 6, -10]
    #
    # l = [1,-4,6,5,-10]
    # print(sorted(l,key = abs,reverse=True)) # [-10, 6, 5, -4, 1]
    # print(sorted(l,key = abs,reverse=False)) # [1, -4, 5, 6, -10]
    # print(sorted([' ',[1,2],'hello world'],key = len,reverse=False)) # [' ', [1, 2], 'hello world']
    # print(sorted([' ',[1,2],'hello world'],key = len,reverse=True)) # ['hello world', ' ', [1, 2]]


    # 匿名函数
    # 函数名 = lambda 参数 : 返回值
    # add = lambda x,y:x+y
    # print(add(1,2))

    # dic = {'k1':10,'k2':20,'K3':30}
    # print(max(dic,key=lambda k:dic[k])) # 筛选字典里面value对应的key
    # Min max map filter sorted都可以和lambda合作

    # #
    # d = lambda p:p*2
    # t = lambda p:p*3
    # x = 2
    # x = d(x)
    # x = t(x)
    # x = d(x)
    # print(x) # 24

    # 现有2个元祖(('a'),('b')),(('c'),('d')),请使用python中的匿名函数生成列表[{'a':'c'},{'b':'d'}]
    # 匿名函数 == 内置函数
    # zip
    # ret = zip((('a'),('b')),(('c'),('d')))
    # res = map(lambda tup:{tup[0]:tup[1]},ret)
    # print(list(res))

    # # 枚举 enumerate
    # for i,j in enumerate(('a','b','c')):
    # print(i,j)
    # 0 a
    # 1 b
    # 2 c

    # for i,j in enumerate(['a','b','c']):
    # print(i,j)
    # 0 a
    # 1 b
    # 2 c

    # for i,j in enumerate({'a':1,'b':2}):
    # print(i,j)
    # 0 a
    # 1 b


  • 相关阅读:
    ASP.NET Core 中间件(Middleware)详解
    .NET Core 使用RSA算法 加密/解密/签名/验证签名
    【Other】希腊诸神大全-中英文名称
    【架构】分布式追踪系统设计与实现
    【架构】SpringCloud 注册中心、负载均衡、熔断器、调用监控、API网关示例
    【SpringCloud】Netflix源码解析之Ribbon:负载均衡策略的定义和实现
    【Docker】基于docker+etcd+confd + haproxy构建高可用、自发现的web服务
    【架构】Kubernetes和Spring Cloud哪个部署微服务更好?
    【Linux】Linux中 “there are stopped jobs”问题的解决方案
    【架构】分布式系统雪崩效应处理方案
  • 原文地址:https://www.cnblogs.com/wonderful0714/p/9508481.html
Copyright © 2011-2022 走看看