zoukankan      html  css  js  c++  java
  • 模块( collections , time , random , os , sys)

    认识模块: 一条代码 < 语句块 < 代码块(函数, 类) < 模块.

    collections (克莱克森斯)

    1. Counter  #用来查看字符出现的次数.
    s = "upup qwe" print(Counter(s))

    队列: FI FO. 先进先出  栈:  FI LO. 先进后出

    2.deque(双向队列)
    from collections import deque
    s = deque()
    s.append("娃哈哈")
    s.append("营养快线")
    s.appendleft("爽歪歪") # 左侧添加
    print(s)
    print(s.pop())  
    print(s.popleft()) #左侧删除
    3.namedtuple (命名元组)
    # from collections import namedtuple
    # nt = namedtuple("point", ["x", "y"])
    # p = nt(1, 2)
    # print(p)
    # print(p.x)
    # print(p.y)

    time 时间模块

    python中时间分成三种表现形式:

     1.  时间戳(timestamp)

    import time
    print(time.time())

    2.  格式化时间(strftime)

    import time
    s = time.strftime("%Y-%m-%d %H:%M:%S") print(s)

     3.  结构化时间(struct_time)

    print(time.localtime())
    结果: time.struct_time(tm_year=2017, tm_mon=05, tm_mday=8, tm_hour=10, tm_min=24, tm_sec=42, tm_wday=0, tm_yday=126, tm_isdst=0)
    所有的转化都要通过结构化时间来转化.

    random 随机数模块

    import random
    print(random.random())  # 0-1小数 
    print(random.uniform(3,10)) 3-10小数 
    print(random.randint(1,10)) 1-10整数
    print(random.randrange(1,10,2)) 1-10奇数 
    print(random.choice([1, '周杰伦', ["盖伦", "胡辣汤"]]))1或者23或者[4,5]) 
    print(random.sample([1, '23', [4, 5]], 2)) 列表元素任意2个组合 
    random.shuffle(lst)  # 随机打乱顺序

    os 和操作系统相关的模块

    import os
    os.makedirs('a/b/c/d') 创建多层
    os.removedirs('dirname1')删除
    os.mkdir('dirname')创建单层
    os.rmdir('dirname')删除单层
    os.rename("oldname","newname")重新命名
    os.getcwd()获取当前⼯作目录

    sys 和python解释器相关的模块

    sys.argv           命令行参数List,第一个元素是程序本身路径
    sys.exit(n)        退出程序,正常退出时exit(0),错误退出sys.exit(1)
    sys.version        获取Python解释程序的版本信息 
    sys.path           返回模块的搜索路径,初始化时使⽤PYTHONPATH环境变量的值 sys.platform       返回操作系统平台名称
  • 相关阅读:
    flink 读取kafka 数据,partition分配
    Flink 报错 "Could not find a suitable table factory for 'org.apache.flink.table.factories.StreamTableSourceFactory' in the classpath"
    flume接收http请求,并将数据写到kafka
    【翻译】Flume 1.8.0 User Guide(用户指南) Processors
    【翻译】Flume 1.8.0 User Guide(用户指南) Channel
    【翻译】Flume 1.8.0 User Guide(用户指南) Sink
    【翻译】Flume 1.8.0 User Guide(用户指南) source
    【翻译】Flume 1.8.0 User Guide(用户指南)
    Apache Flink 简单安装
    Java之使用IDE
  • 原文地址:https://www.cnblogs.com/asdlo/p/9757528.html
Copyright © 2011-2022 走看看