Fedora31学习笔记(五)继续学习python:随机数、时间和日期、再谈列表和字典

Fedora31学习笔记(五)继续学习python:随机数、时间和日期、再谈列表和字典

一、随机数

import random

# 从[1, 100]中生成随机列表,可重复
a = [random.randint(1, 100) for x in range(10)]
print(a)
# 从[1, 100]中生成随机列表,不重复
b = random.sample(range(1, 101), 10)
print(b)
# 随机选择一个元素
c = random.choice(a)
print(c)
d = b[random.randint(0, 9)]
print(d)

输出(随机的):

[86, 82, 73, 47, 86, 2, 42, 51, 69, 55]
[73, 44, 15, 100, 53, 37, 32, 63, 1, 70]
73
37

二、时间和日期

注意:以下代码省略了print(懒,聪明),但可以直接copy到Ipython和Jupyter中运行。

import time

# time()  从纪元(1970.1.1 0:00) 开始的秒数
# ctime() 人类可读的本地时间
time.time(), time.ctime()

输出:(1588241917.8097813, 'Thu Apr 30 18:18:37 2020')

struct_time 格式

# struct_time 格式
# gmtime()返回UTC时间,即世界时。北京时间 = UTC + 8h
type(time.gmtime()), \
type(time.localtime()), \
time.gmtime(), \
time.localtime()

输出:

(time.struct_time,
 time.struct_time,
 time.struct_time(tm_year=2020, tm_mon=4, tm_mday=30, tm_hour=15, tm_min=4, tm_sec=43, tm_wday=3, tm_yday=121, tm_isdst=0),
 time.struct_time(tm_year=2020, tm_mon=4, tm_mday=30, tm_hour=23, tm_min=4, tm_sec=43, tm_wday=3, tm_yday=121, tm_isdst=0))

获取年、月、日、时、分、秒

t = time.localtime()
t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec

输出: (2020, 4, 30, 18, 8, 36)

时间格式转换

def show_struct(s):
    print('  tm_year :', s.tm_year)
    print('  tm_mon  :', s.tm_mon)
    print('  tm_mday :', s.tm_mday)
    print('  tm_hour :', s.tm_hour)
    print('  tm_min  :', s.tm_min)
    print('  tm_sec  :', s.tm_sec)
    print('  tm_wday :', s.tm_wday)
    print('  tm_yday :', s.tm_yday)
    print('  tm_isdst:', s.tm_isdst)

# 时间格式的转化
# ts to ctime()'s str
ts = 1588241917.8097813
t_str = time.ctime(ts)
print('ts to str: ', t_str)
# str to struct_time
t_struct = time.strptime(t_str)
print('str to struct_time: ', t_struct)
show_struct(t_struct)
# struct_time to str
print('struct_time to str: ', time.strftime("%Y-%m-%dT%H:%M:%S", t_struct))
# ts to struct_time
t_struct2 = time.localtime(ts)
print('ts to struct_time: ', t_struct2)
# struct_time to ts
ts1 = time.mktime(t_struct2)
print('struct_time to ts: ', ts1)
# str to ts:
ts2 = time.mktime(time.strptime(t_str))
print('str to ts: ', ts2)

输出:

ts to str:  Thu Apr 30 18:18:37 2020
str to struct_time:  time.struct_time(tm_year=2020, tm_mon=4, tm_mday=30, tm_hour=18, tm_min=18, tm_sec=37, tm_wday=3, tm_yday=121, tm_isdst=-1)
  tm_year : 2020
  tm_mon  : 4
  tm_mday : 30
  tm_hour : 18
  tm_min  : 18
  tm_sec  : 37
  tm_wday : 3
  tm_yday : 121
  tm_isdst: -1
struct_time to str:  2020-04-30T18:18:37
ts to struct_time:  time.struct_time(tm_year=2020, tm_mon=4, tm_mday=30, tm_hour=18, tm_min=18, tm_sec=37, tm_wday=3, tm_yday=121, tm_isdst=0)
struct_time to ts:  1588241917.0
str to ts:  1588241917.0

继续探索

import datetime

d = datetime.datetime.now()
type(d), d, datetime.datetime.utcnow()

输出:

(datetime.datetime,
 datetime.datetime(2020, 4, 30, 23, 8, 12, 79732),
 datetime.datetime(2020, 4, 30, 15, 8, 12, 79765))

type(time.time()), type(time.ctime()), type(time.localtime()), type(datetime.datetime.now())
输出: (float, str, time.struct_time, datetime.datetime)

datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
输出: '2020-04-30T15:09:20'

datetime.datetime.now().isoformat("T", "milliseconds") + 'Z'
输出: '2020-04-30T23:11:56.637Z'

三、再谈列表和字典

再谈列表

下标访问(可以用负数,但也不可以超出范围)

a = [x for x in range(1, 10+1)]     # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[0], a[3], a[9], a[-1], a[-7], a[-10]

输出: (1, 4, 10, 10, 4, 1)
另外,a[10] 和 a[-11] 都是产生错误 IndexError: list index out of range

切片(不指向原列表,互不影响)

# 从start(含)开始,到end(不含)结束
start, end = 2, 5       #  [3, 4, 5]
a[start:end]    
# start, end = 2, 1     #  []
# start, end = 1, -10   #  []

输出: [3, 4, 5]

再谈字典

用列表、元组初始化字典

d = {}
d['a'], d['b'] = [x for x in range(2)]   
d

输出: {'a': 0, 'b': 1}

应习惯于用d.get('key')代替 d['key']

# print(d['c'])     # error: KeyError
print(d.get('c'))   # ok  结果是None,这样可以避免在获取不存在的键时出现错误而退出。

组合生成字典

import string
string.ascii_lowercase

输出: 'abcdefghijklmnopqrstuvwxyz'

d2 = dict(zip(string.ascii_lowercase, [x for x in range(1, 26+1)]))
print(d2)

输出: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}

下期预告:继续学习Python...

猜你喜欢

转载自www.cnblogs.com/qydw000/p/12805124.html