zoukankan      html  css  js  c++  java
  • python学习 day15 (3月20日)----time

    # '2019-03-20 10:40:00'
    #这个时间向后推一个月
    f1 = time.strptime('2019-03-20 10:40','%Y-%m-%d %H:%M')
    # 把字符串时间转换成 结构化时间  
    # time.struct_time(tm_year=2019,
    a_time = time.mktime(f1)+30*86400  #1555641600.0
    # 把结构化转换成时间戳时间 加上一个月的时间
    af_time = time.localtime(a_time)
    #把时间戳时间转换成 结构化时间
    # time.struct_time(tm_year=2019, tm_mon=4, 

    print(time.strftime("%Y-%m-%d %H:%M:%S",af_time))#2019-04-19 10:40:00
    #获取当前时间向前一个月的现在时间
    # 获取时间戳时间  #减去一个月的时间
    a = time.time()-30*86400  #1553051329.3784719  #1550459329.3784719  
    new = time.localtime(a)
    # 把时间戳转换成结构化时间  #time.struct_time(tm_year=2019, tm_mon=2print(time.strftime("%Y-%m-%d %X",new))
    # 把结构化转换 字符串时间  #2019-02-18 11:08:49

    总结:

    #总结:
        # 6个
        # 时间戳  -- 结构化 1   time.localtime(shi)
        # 结构化  -- 时间戳 1   time.mktime(jie)
        #结构化   -- 字符串 1   time.strftime("%Y-%m-%d %X",jie)
        #字符串   -- 结构化 1   time.strptime('2019-03-20 10:40','%Y-%m-%d %H:%M')
        #time.time()       1
        #strftime()       1   #获取当前字符串时间

     

     datetime

    from datetime import datetime,timedelta#从XX导入 建议
    # import datetime.datetime
    
    print(datetime.now())#2019-03-20 11:36:03.263924(毫秒)
    #时间对象(类似于字符串时间)
    print(datetime.timestamp(datetime.now())) #将时间对象转换成时间戳#1553053518.409677
    #求时间戳 比time.time()慢
    f = datetime.timestamp(datetime.now())
    print(datetime.fromtimestamp(f))# 将时间戳转换成时间对象#2019-03-20 11:43:44.072281
    
    print(datetime.strptime('2018-11-30','%Y-%m-%d'))#2018-11-30 00:00:00
    #将字符串转成时间对象
    print(datetime.strftime(datetime.now(),'%Y-%m-%d'))# 后格式 #2019-03-20
    #将时间对象转成字符串
    
    print(datetime.now()-timedelta(hours=10))#2019-03-20 01:43:44.084282
    print(datetime.now()-timedelta(weeks=10))#2019-03-10 11:45:18.421678
    #这个是datetime的精华
    View Code
    collections
    #数据类型补充
    from collections import Counter
    #计算
    print(Counter([1,3,4,5,6,3,222,3,4,67,7,3,4,]))
    #Counter({3: 4, 4: 3, 1: 1, 5: 1, 6: 1, 222: 1, 67: 1, 7: 1})
    
    print(Counter('adasdfasdfasdfsafd'))
    # Counter({'a': 5, 'd': 5, 's': 4, 'f': 4})
    print(Counter((1,3,4,5,6,3,222,3,4,67,7,3,4,)))
    c = Counter('adasdfasdfasdfsafd')
    print(c[1])  # 0  都是 0
    print(dict(c))  # 对象类型 Counter 就可以转 dic
    #{'a': 5, 'd': 5, 's': 4, 'f': 4}
    from collections import namedtuple
    # 命名元组
    #
    tu =namedtuple('liuyang',['name','age','sex','hobby'])
    print(tu('shuaige',19,'nan','chouyan'))
    #liuyang(name='shuaige', age=19, sex='nan', hobby='chouyan')
    # 元组  数目比较多  明确的表明每一个元素是什么意思
    print(tu,tu.name)#<class '__main__.liuyang'> #<property object at 0x000000000289D688>
    # property
    t = tu('shuaige',19,'nan','chouyan')
    #实例一个对象
    #对象找属性
    print(t.age , t[0])# 19 shuaige
    
    class A:
        @property
        def func(self):
            print(11)
    a = A()
    a.func  #11
    
    from collections import deque
    #双端队列
    d = deque([1,2,3,4])
    d.append(5)     #右边添加
    # deque([1, 2, 3, 4, 5])
    d.appendleft(10) #左边添加
    # deque([10, 1, 2, 3, 4, 5])
    d.insert(2,6)
    # deque([10, 1, 6, 2, 3, 4, 5])
    print(d.pop())#5
    print(d.popleft())#10
    d.remove(2)
    print(d)    #deque([1, 6, 3, 4])
    View Code
    #(1)队列 (2)栈
    #(1)FIFO 先进先出   #两个门???
    #(2)      后进后出   先进后出(坐电梯)
    
    from collections import defaultdict
    #默认字典
    
    d = defaultdict(list)
    print(d)#defaultdict(<class 'list'>, {})
    # d['name']
    print(d)  #defaultdict(<class 'list'>, {'name': []})
    # 因为上面的规定defaultdict(list)
    # dic = {}
    # dic['name']
    # print(dic)    #KeyError: 'name'
    
    li = [
        {'name':'alex','hobby':'抽烟'},
        {'name': 'alex', 'hobby': '喝酒'},
        {'name': 'alex', 'hobby': '烫头'},
        {'name': 'alex', 'hobby': '哈哈'},
        {'name': 'wusir', 'hobby': '游泳'},
        {'name': 'wusir', 'hobby': '打牌'},
        {'name': 'wusir', 'hobby': '小包间'},
        {'name': '太白', 'hobby': '洗脚'},
        {'name': '太白', 'hobby': '开车'},
    ]
    for i in li :
        d[i['name']].append(i['hobby'])
    
    print([dict(d)])
    
    li = [('红色',1),('黄色',1),('绿色',1),('红色',1)]
    
    d = defaultdict(list)
    for i in li:
        d[i[0]].append(i[1])
    print(d)
    dd = dict(d)
    for i in dd:
        dd[i]   = sum(dd[i])
    print(dd)
    
    #键是一样的  值也是一样的 方便
    import time
    dic = {"1":2,"2":3,"3":5,'alex':1,'a':3,'sfad':3,'neja':4}
    # while 1:
    #     time.sleep(0.5)
    #     print(dic)
    '''
    {'1': 2, '2': 3, '3': 5}
    {'1': 2, '2': 3, '3': 5}
    {'1': 2, '2': 3, '3': 5}
    {'1': 2, '2': 3, '3': 5}'''
    print(dic)
    View Code
    # collections
    #deque:双端队列
        #两头都可以删除
        #队列  先进先出
        #栈  新进后出
    #Counter:计算
    #    计数 : 统计元素出现次数,字典形式显示
    #   #Counter({1:2}) 这种可以直接dict()转换
    #defaultdict:默认字典
    #   利用他的特性
    #namedtuple :命名元祖
    #   作用:将元祖中的元素进行标明(让别人知道这元素是什么意思)
  • 相关阅读:
    PHP上传文件
    PHP文件系统操作常用函数
    利用ini_set()函数实现对php配置文件的修改
    文本文件合并(C++实现)
    手把手教你在新浪云上免费部署自己的网站--连接数据库
    手把手教你在新浪云上免费部署自己的网站---基础
    update2
    在Windows命令行中编译运行C/C++程序
    【转】CSS颜色代码大全
    (html4与html5分别实现)用一个表单向php界面提交不同的命令
  • 原文地址:https://www.cnblogs.com/Doner/p/10563817.html
Copyright © 2011-2022 走看看