Task08:模块与datetime模块
练习题:
1、怎么查出通过 from xx import xx导⼊的可以直接调⽤的⽅法?
在test_package文件夹中创建一个 init.py 的模块文件,内容可以为空。然后在代码中把test_package的文件的路径加入到python解释器可以搜索到的路径列表中。
2、了解Collection模块,编写程序以查询给定列表中最常见的元素。
题目说明:
输入:language = [‘PHP’, ‘PHP’, ‘Python’, ‘PHP’, ‘Python’, ‘JS’, ‘Python’, ‘Python’,‘PHP’, ‘Python’]
输出:Python
"""
Input file
language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
Output file
Python
"""
def most_element(language):
""" Return a list of lines after inserting a word in a specific line. """
# your code here
模块:
# collections.py
def most_element(language):
a=0
b=0
for i in range(len(language)):
for j in range(len(language)-1):
if language[i]==language[j+1]:
a=a+1
if a>b:
b=a
cc=language[i]
return cc
import collections
language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
print(collections.most_element(language))
练习题:
1、假设你获取了用户输入的日期和时间如2020-1-21 9:01:30
,以及一个时区信息如UTC+5:00
,均是str
,请编写一个函数将其转换为timestamp:
题目说明:
"""
Input file
example1: dt_str='2020-6-1 08:10:30', tz_str='UTC+7:00'
example2: dt_str='2020-5-31 16:10:30', tz_str='UTC-09:00'
Output file
result1: 1590973830.0
result2: 1590973830.0
"""
import datetime
import re
from dateutil import parser
dt_str=‘2020-6-1 08:10:30’
tz_str=‘UTC+7:00’
def to_timestamp(dt_str, tz_str):
# your code here
dt = parser.parse(dt_str)
tz_r = re.match(r’^UTC([+|-]\d{1,2}):00$’, tz_str)
tz = datetime.timezone(datetime.timedelta(hours=int(tz_r.group(1))))
dt = dt.replace(tzinfo=tz)
return dt.timestamp()
pass
2、编写Python程序以选择指定年份的所有星期日。
题目说明:
"""
Input file
2020
Output file
2020-01-05
2020-01-12
2020-01-19
2020-01-26
2020-02-02
-----
2020-12-06
2020-12-13
2020-12-20
2020-12-27
"""
from datetime import date, timedelta
def all_sundays(year):
# your code here
d = date(year, 1, 1) # January 1st
d += timedelta(days = 6 - d.weekday()) # First Sunday
while d.year == year:
yield d
d += timedelta(days = 7)
for d in allsundays(2020):
print(d)