模块与datetime模块
1、怎么查出通过 from xx import xx导⼊的可以直接调⽤的⽅法?
(1)在test_package文件夹中创建_init_.py文件,里边什么都不需要编辑。
(2)在代码中把test_package的文件的路径加入到python解释器可以搜索到的路径列表中,这里就用到了python的包sys模块。
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. “”"
def most_element(language):
from collections import Counter
language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
a=Counter(language)
print(a)#Counter({'Python': 5, 'PHP': 4, 'JS': 1})
max_value = max(dict.values(a))
print(max_value)#5
for keys,values in dict.items(a):
if values==max_value:
print('出现次数最多的是:',keys,'出现次数为',values,'次')
most_element(language)
1、假设你获取了用户输入的日期和时间如2020-1-21
9:01:30,以及一个时区信息如UTC+5:00,均是str,请编写一个函数将其转换为timestamp:
import datetime
from dateutil import tz, zoneinfo
def to_timestamp(dt_str, tz_str):
[year, month, day] = [int(i) for i in dt_str.split(' ')[0].split('-')]
[hour,mint, sec] = [int(i) for i in dt_str.split(' ')[1].split(':')]
t = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=mint, second=sec, tzinfo = tz.gettz(tz_str))
return t.timestamp()
2、编写Python程序以选择指定年份的所有星期日。
import datetime
def all_sundays(year):
n=int(a)
dt1=datetime.date(n,1,1)
dt2=datetime.date(n,12,31)
for i in range((dt2-dt1).days+1):
day=dt1+datetime.timedelta(days=i)
b=day.isoweekday()
if b==7:
print(day)
else:
continue