目录
模块
作业
1、怎么查出通过 from xx import xx导⼊的可以直接调⽤的⽅法?
可以使用help函数查看导入的方法
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
def most_element(language):
a = 0
for i in language:
if language.count(i) > a:
a = language.count(i)
max_elem = i
return max_elem
language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
print(most_element(language))
#Python
datetime模块
作业
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
"""
def to_timestamp(dt_str, tz_str):
# your code here
pass
import datetime
from dateutil import parser
def to_timestamp(dt_str,tz_str):
dt = parser.parse(dt_str)
tz = re.match(r'^UTC([+-])(\d+):(\d+)', tz_str)
tz1 = tz.group(1)
tz2 = int(tz.group(2))
if tz1 == '+':
tz_utc = timezone(timedelta(hours=tz2))
else:
tz_utc = timezone(timedelta(hours=-tz2))
dt = dt.replace(tzinfo=tz_utc)
return dt.timestamp()
t1 = to_timestamp('2020-6-1 08:10:30', 'UTC+7:00')
print(t1)
t2 = to_timestamp('2020-5-31 16:10:30', 'UTC-09:00')
print(t2)
#1590973830.0
#1590973830.0
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
"""
def all_sundays(year):
# your code here
import datetime
def all_sunday(year):
year = int(year)
dt = datetime.date(year,1,1)
dt_start = dt + datetime.timedelta(days =(7 - dt.isoweekday()))
while dt_start.year == year:
print(dt_start)
dt_start += datetime.timedelta(weeks = 1)
all_sunday(2020)
"""
2020-01-05
2020-01-12
2020-01-19
2020-01-26
2020-02-02
2020-02-09
2020-02-16
2020-02-23
2020-03-01
2020-03-08
2020-03-15
2020-03-22
2020-03-29
2020-04-05
2020-04-12
2020-04-19
2020-04-26
2020-05-03
2020-05-10
2020-05-17
2020-05-24
2020-05-31
2020-06-07
2020-06-14
2020-06-21
2020-06-28
2020-07-05
2020-07-12
2020-07-19
2020-07-26
2020-08-02
2020-08-09
2020-08-16
2020-08-23
2020-08-30
2020-09-06
2020-09-13
2020-09-20
2020-09-27
2020-10-04
2020-10-11
2020-10-18
2020-10-25
2020-11-01
2020-11-08
2020-11-15
2020-11-22
2020-11-29
2020-12-06
2020-12-13
2020-12-20
2020-12-27
"""
参考资料:
[1] https://www.liaoxuefeng.com/wiki/1016959663602400/1017648783851616#0