# import collections from collections import Counter # s = "I am sylar, I have a dream, freedom...." # dic = {} # for el in s: # dic[el] = dic.setdefault(el, 0) + 1 # print(dic) # # qq = Counter(s) # # print("__iter__" in dir(qq)) # for item in qq: # print(item, qq[item]) # lst = ["五花马", "千金裘", "不会", "不会", "不会"] # c = Counter(lst) # print(c['五花马']) # dic = {"a":"b", "c":"d"} # print(dic.keys()) # 栈 特点:先进后出 # 队列 特点:先进先出 # python中提供了队列. 没有栈. 自己写一个栈 # class StackFullError(Exception): # pass # class StackEmptyError(Exception): # pass # # class Stack: # def __init__(self, size): # self.index = 0 # 栈顶指针 # self.lst = [] # self.size = size # # # 给栈添加元素 # def push(self, item): # if self.index == self.size: # # 栈已经满了. 不能再装东西了 # raise StackFullError('the stack is full') # self.lst.insert(self.index, item) # 对于空列表. 需要insert插入内容 # # self.lst[self.index] = item # 把元素放到栈里 # self.index += 1 # 栈顶指针向上移动 # # # 从栈中获取数据 # def pop(self): # if self.index == 0: # raise StackEmptyError("the stack is empty") # self.index -=1 # 指针向下移动 # item = self.lst.pop(self.index) # 获取元素. 删除. # return item # s = Stack(5) # s.push("馒头1号") # s.push("馒头2号") # s.push("馒头3号") # s.push("馒头4号") # s.push("馒头5号") # # print(s.pop()) # print(s.pop()) # print(s.pop()) # print(s.pop()) # print(s.pop()) # # # lst = [] # lst.append("哈哈1") # lst.append("哈哈2") # lst.append("哈哈3") # lst.append("哈哈4") # # print(lst.pop()) # print(lst.pop()) # print(lst.pop()) # print(lst.pop()) # 队列 # import queue # # # q = queue.Queue() # 创建队列 # q.put("李嘉诚") # q.put("陈冠希") # q.put("周润发") # q.put("吴彦祖") # # print(q.get()) # print(q.get()) # print(q.get()) # print(q.get()) # # print(q.get()) # 队列中如果没有元素了. 继续获取的话. 会阻塞 # print("拿完了") # from collections import deque # # q = deque() # 创建一个双向队列 # q.append("高圆圆") # q.append("江疏影") # q.appendleft("赵又廷") # q.appendleft("刘大哥") # # 刘大哥 赵又廷 高圆圆 江疏影 # print(q.pop()) # 从右边获取数据 # print(q.pop()) # print(q.popleft()) # 从左边获取数据 # print(q.popleft()) # print(q.pop()) # from collections import namedtuple # # point = namedtuple("Point", ["x", "y", 'z']) # 这个就相当于写了一个类 # # class point: # # def __init__(self, x, y): # # self.x = x # # self.y = y # # p = point(5, 18, 88) # print(p.x) # print(p.y) # # p.x = 19 # 终归是一个元组 # print(p) # dic = {'a':'娃哈哈', 'b':'薯条', 'c':'胡辣汤'} # print(dic) # 最底层一定是无序的. 最底层是hash # # from collections import OrderedDict # # 按照我们存储的顺序保存数据 # od = OrderedDict({ 'b':'薯条','a':'娃哈哈', 'c':'胡辣汤'}) # print(od) # dic = {} # # print(dic["周润发"]) # 报错 # print(dic.get("周润发", "英雄本色")) # None from collections import defaultdict # d = defaultdict(list) # {} # 参数位置给的内容必须是可调用的 # d["周杰伦"] = "昆凌" # print(d["周杰伦"]) # 从字典中获取数据的时候. 如果这个key不存在. 去执行可执行的内容, 拿到的是一个空列表 # lst= [11,22,33,44,55,66,77,88,99] # d = defaultdict(list) # for el in lst: # if el < 66: # d["key1"].append(el) # key1默认是不存在的. 但是可以拿key1. 一个空列表. # else: # d["key2"].append(el) # print(d) def func(): return "胡辣汤" d = defaultdict(func) print(d["哈哈"]) print(d)
import time # 中文 import locale locale.setlocale(locale.LC_CTYPE, "chinese") # 获取当前系统时间, 时间戳 # print(time.time()) # 1542166230.6139991, 给机器看的, 以1970-01-01 00:00:00 数据库存储的是这个时间 # 格式化时间 2018-11-14 11:22:56 2018/11/14 11:22:56 # s = time.strftime("%Y-%m-%d %H:%M:%S") # string format time # print(s) # 2018-11-14 11:42:06 # # time.sleep(5) # 睡, 到点起床 wait() -> notify() # print("起床了") # while 1: # s = time.strftime("%Y-%m-%d %H:%M:%S") # 使用频率最高 # print(s) # time.sleep(1) # t = time.localtime() # print(t.tm_year) # print(t.tm_mon) # print(t.tm_mday) # print(t.tm_hour) # print(t.tm_min) # print(t.tm_sec) # print("%s-%s-%s %s:%s:%s" % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)) # 数据库里存了一个时间戳 1888888888 # 从时间戳 -> 格式化时间 t = time.localtime(1542513992) # 时区 gmtime() 格林尼治时间. print(t) str_time = time.strftime("%Y-%m-%d %H:%M:%S", t) print(str_time) # 用户输入一个时间. 变成时间戳 # 格式化时间 -> 时间戳 # 2018-11-18 12:06:32 # s = "2018-11-18 12:06:32" # t = time.strptime(s, "%Y-%m-%d %H:%M:%S") # string parse time # print(t) # # 结构化时间 -> 时间戳 # ss = time.mktime(t) # print(ss) # print(time.strftime("%Y年%m月%d日")) # # # 中文 # import locale # locale.setlocale(locale.LC_CTYPE, "chinese") # 时间差 1小时30分 # begin = "2018-11-14 16:30:00" # end = "2018-11-14 18:00:00" # # 用时间戳计算出时间差(秒) # begin_struct_time = time.strptime(begin, "%Y-%m-%d %H:%M:%S") # end_stract_time = time.strptime(end, "%Y-%m-%d %H:%M:%S") # # begin_second = time.mktime(begin_struct_time) # end_second = time.mktime(end_stract_time) # # # 秒级的时间差 180000 # diff_time_sec = abs(begin_second - end_second) # # # 转换成分钟 # diff_min = int(diff_time_sec//60) # print(diff_min) # # diff_hour = diff_min//60 # 1 # diff_min_1 = diff_min % 60 # 30 # # print("时间差是 %s小时%s分钟" % (diff_hour, diff_min_1)) # begin = "2019-11-14 16:30:00" # end = "2018-11-14 18:00:00" # # 用时间戳计算出时间差(秒) # begin_struct_time = time.strptime(begin, "%Y-%m-%d %H:%M:%S") # end_stract_time = time.strptime(end, "%Y-%m-%d %H:%M:%S") # # begin_second = time.mktime(begin_struct_time) # end_second = time.mktime(end_stract_time) # # # 秒级的时间差 180000 # diff_time_sec = abs(begin_second - end_second) # # # 转化成结构化时间 # t = time.gmtime(diff_time_sec) # 最好用格林尼治时间。 否则有时差 # print(t) # # print("时间差是%s年%s月 %s天 %s小时%s分钟" % (t.tm_year-1970, t.tm_mon-1, t.tm_mday-1,t.tm_hour, t.tm_min )) # 自学:datetime(时间), calendar(日历) t = time.localtime() print(type(t)) # (2018, 11, 18, 12, 6, 32,6, 322, 0) from collections import namedtuple p = namedtuple("Point",['x', 'y']) pp = p(1,2) print(pp) print(type(pp))
import random # print(random.randint(1,2)) # [start, end] # print(random.random()) # (0,1)之间的小数 # print(random.uniform(3,10)) # (3, 10 )的随机小数 # n = random.randrange(1, 10, 3) # [1, 10) 从奇数中获取到随机数 # while n != 10: # n = random.randrange(1, 10, 3) # for i in range(1, 10, 3): # print(i) print(random.choice([1, '周杰伦', ["盖伦", "胡辣汤"]])) # print(random.sample([1, '23', [4, 5]], 2)) # 列表元素任意2个组合 lst = ["周杰伦", "昆凌", "马化腾", "马丽", "沈腾", "秋雅"] random.shuffle(lst) print(lst)
# os.makedirs('dirname1/dirname5') # 创建文件夹目录结构 # os.removedirs('dirname1/dirname5') # 删除文件夹, 如果文件夹内没有东西。 就可以删除。 否则报错 # os.mkdir('dirname/哈哈') # mkdir如果父级目录不存在。 报错 # os.rmdir('dirname') # 删除文件夹 # print(os.listdir('../')) # 获取到文件夹内的所有内容. 递归 # print(os.stat('dirname')) # linux # os.system("dir") # 直接执行命令行程序 # s = os.popen("dir").read() # print(s) # print(os.getcwd() ) # 当前程序所在的文件夹 # # print(os.path.abspath("../day020 继承") ) # 获取绝对路径 # print(os.path.split("D:python_workspaceday020 继承")) # 拆分路径 ('D:\python_workspace', 'day020 继承') # print(os.path.dirname("D:python_workspaceday020 继承")) # D:python_workspace # print(os.path.basename("D:python_workspaceday020 继承")) # day020 继承 # # print(os.path.exists("dirname")) # 判断文件是否存在 # print(os.path.isabs("D:python_workspaceday020 继承")) # 是否是绝对路径 # # print(os.path.isfile("01 今日主要内容")) # 是否是文件 # print(os.path.isdir("dirname")) # 是否是文件夹 # # print(os.path.getsize("01 今日主要内容") ) # 文件大小 # print("胡辣汤", "传盛", "big", sep="small") # print("c:"+os.sep+"胡辣汤") # \/ 文件路径的分隔符 # print(os.name) # nt import sys # sys.exit(1) # 正常退出 # print(sys.version) # print(sys.platform) # 平台名称 print(sys.path) # 搜索模块的路径 sys.path.append("e:/") import master master.chi()