Day27

 

1、collections

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

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

# 默认字典
# 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
# 将所有大于 66 的值保存至字典的第一个key中,
# 将小于 66 的值保存至第二个key的值中。
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['k2']=[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)
View Code
from collections import defaultdict
d=defaultdict(set)
print(d)
print(d['a'])
d['b']=10
print(d)
View Code
{}默认的value是5
from collections import defaultdict
d=defaultdict(lambda :5)
print(d[1])
print(d)
View Code
from collections import Counter
c = Counter('abcdeabcdabcaba')
print(c)
View Code
class Configparser:
    def __init__(self,section,option):
        self.section = section
        self.option = option
    def write(self,f):
        f.write(self.section,self.option)

f = open('test','w')
config = Configparser('a','b')
config.write(f)
View Code

2、time模块

记录时间的格式:

  给人看的

  给机器看的

  计算用的

时间戳时间:给机器看的

print(time.time())

格式化时间:给人看的

%y 两位数的年份表示(00-99%Y 四位数的年份表示(000-9999%m 月份(01-12%d 月内中的一天(0-31%H 24小时制小时数(0-23%I 12小时制小时数(01-12%M 分钟数(00=59%S 秒(00-59%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
View Code

print(time.strftime('%Y-%m-%d %H:%M%S'))  字符串格式化时间

结构化时间

索引(Index) 属性(Attribute) 值(Values)
0 tm_year(年) 比如2011
1 tm_mon(月) 1 - 12
2 tm_mday(日) 1 - 31
3 tm_hour(时) 0 - 23
4 tm_min(分) 0 - 59
5 tm_sec(秒) 0 - 60
6 tm_wday(weekday) 0 - 6(0表示周一)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否是夏令时) 默认为0

猜你喜欢

转载自www.cnblogs.com/a352735549/p/8931369.html