day27模块

collectons模块

from collections import OrderedDict
# d = OrderedDict()
# d['a'] = 1
# d['z'] = 2
# d['b'] = 3
# print(d) #有序的
#d['z'] = 0 #改
#print(d)

 默认字典

  例题:有如下值集合[11,22,33,44,55,66,77,88,99,90],将大于66的值保存至第一个key的值中,将小于66的值保存至第二个key的之中。

# dic = {}
# dic.setdefault('k',[])
# print(dic)


# dic = {}
# l = [11,22,33,44,55,66,77,88,99,90]
# for i in l :
#     if i > 66:
#         if dic.get('k1'):
#             dic['k1'].append(i)
#         else:
#             dic['k1']=[i]
#     elif i < 66 :
#         if dic.get('k2'):
#             dic['k2'].append(i)
#         else:
#             dic['ke'] = [i]
# print(dic)


# from collections import defaultdict
# d = defaultdict(list)
# l = [11,22,33,44,55,66,77,88,99,90]
# for i in l:
#     if i > 66:
#         d['k1'].append(i)
#     elif i < 66:
#         d['k2'].append(i)
# print(d)
# from collections import defaultdict
# d = defaultdict(lambda :5)
# print(d[1])
# print(d)
# from collections import Counter
# c = Counter('abcdeabcdabcd')
# print(c)

  默认字典既可以让它的默认值是一个字典,列表,集合,也可以让它的默认值是数字,字符串。

默认字典最大的好处是 永远不会在你使用可以获取值的时候报错。

默认字典是给字典中的value设置默认值。 

counter模块

# from collections import Counter
# c = Counter('abcdeabcdabcaba')
# print(c)

 collections模块

# class Configparser:
#     def __init__(self,section,option):
#         self.section = section
#         self.option = option
#     def write(self,f):
#         f.write(self.section)
# f = open('test','w')
# config = Configparser('a','b')
# config.write(f)


# import configparser
# configparser.ConfigParser.write()

  item模块

import time

time.time() 微软的时间同步服务器

时间

计算执行代码的时间 time.time()

让程序停在这里一段时间 sleep

记录时间的格式:

  给人看的

  给机器看的

  计算用的

time.sleep(set)

推迟指定的时间运行,单位为秒

time.time()

获取当前时间戳

# import time
# 时间戳时间
# print(time.time())   # 时间戳时间   # 计算时间差  精准的运算
#格式化时间
# print(time.strftime('%Y-%m-%d %H:%M:%S'))# 字符串格式化时间   # 记录下来给人看
# print(time.strftime('%y-%m-%d'))# 字符串格式化时间
# print(time.strftime('%x %X'))# 字符串格式化时间
# print(time.strftime('%c'))# 字符串格式化时间
#结构化时间
# print(time.localtime())# 本地结构化时间        # 时间戳时间转格式化时间的中间件
                                                # 对应项的简单计算
# print(time.gmtime())   # 英国的结构化时间

 结构化时间是格式化时间和时间戳的中间值

例题

2015-8-8时间戳时间

# p = time.strptime('2015-8-8','%Y-%m-%d')
# print(p)
# print(time.mktime(p))
# print(time.time()-time.mktime(p))

# print(time.time())
#
# ret = time.localtime(1500000000)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret))

# ret = time.localtime(2000000000)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret))

# ret = time.localtime(3000000000)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret))  #2033   2065    32

# ret = time.localtime(0)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret))  #1970 1 1 8
#
# ret = time.gmtime(0)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret))  #1970 1 1 0


# print(time.strftime('%c'))
# print(time.ctime(1500000000))

# ret = time.localtime(2000000000)
# print(ret)
# print(time.asctime())
# print(time.asctime(ret))

 sys模块和os模块

# sys.argv           命令行参数List,第一个元素是程序本身路径
# sys.exit(n)        退出程序,正常退出时exit(0),错误退出sys.exit(1)
# sys.version        获取Python解释程序的版本信息
# sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
# sys.platform       返回操作系统平台名称
# import sys
# print('*'*6)
# sys.exit()      #退出程序,正常退出时exit(0),错误退出sys.exit(1)
# print('-'*6)
# import sys
# print(sys.version)

# import sys
# print(sys.platform)

# import sys    # *****
# print(sys.path)

 内存:

  程序在运行的时候

  启动解释器加载一个基础的内容,内置函数,内置模块-->内存里

#  sys.path 系统的路径   import
# import os,sys

# import sys
# print(sys.argv)   # 列表 列表的第一项是当前文件所在的路径
# import sys
# user = input('>>>')
# pwd = input('>>>')
# if user == 'alex' and pwd == '3714':
#     print('登录成功')
# else:
#     sys.exit()
# print('我能完成的功能')
# import sys
# if sys.argv[1] == 'alex' and sys.argv[2] == '3714':
#     print('登录成功')
# else:
#     sys.exit()
# import logging
# num = int(input('>>>'))
# logging.debug(num)
# a = num*100
# logging.debug(num)
# b = a - 10
# logging.debug(num)
# c = b + 5
# print(c)

# import sys
# import logging
# inp = sys.argv[1] if len(sys.argv) > 1 else 'WARNING'
# logging.basicConfig(level=getattr(logging,inp))
# num = int(input('>>>'))
# a = num * 100
# logging.debug(a)
# b =a - 10
# logging.debug(b)
# c = b + 5
# print(c)

 rondom  模块(随机)打乱顺序

# print(random.random())   # 0-1随机小数
# print(random.uniform(1,4))

# print(random.randint(1,1000))    # 包含1,2
# print(random.randrange(1,2))     # 不包含2
# print(random.randrange(1,20,2))  # 包含所有奇数

# print(random.choice([1,'23',[4,5]]))

# print(random.sample([1,'23',[4,5]],2))

# item=[1,3,5,7,9]
# import random
# random.shuffle(item)
# print(item)

猜你喜欢

转载自www.cnblogs.com/hanjiahao/p/8934269.html
今日推荐