python基础——task8打卡

练习题一:

1、怎么查出通过 from xx import xx导⼊的可以直接调⽤的⽅法?
  • 利用help函数查看function
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

from collections import Counter
language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
def most_element(language):
    x= Counter(language) 
    dic = dict(x) 
    list1 = list(dic.keys()) 
    list2 = list(dic.values()) 
    i = list2.index(max(list2)) 
print(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

	def to_timestamp(dt_str, tz_str):
    	dt1=datetime.datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
    	utc_group = re.match(r'([UTC]+)([+-])(\d+):(\d)',tz_str)#字符串匹配,分组
    	i = int(utc_group.group(3))#时区数字
    	if utc_group.group(2) == '+':#时区正负号
        	tz_utc = timezone(timedelta(hours=i))
    	elif utc_group.group(2) == '-':
        	tz_utc = timezone(timedelta(hours=-i))
    	dt = dt1.replace(tzinfo=tz_utc)
    	return dt.timestamp()

	dt_str = input("请输入日期和时间(格式:1990-1-1 00:00:00):")
	tz_str = input("请输入时区信息(格式:UTC+7:00):")
	print(to_timestamp(dt_str,tz_str))
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 dateutil import parser

def all_sundays(year):
    day = '-01-01'
    dt=year+day
    dt1 = parser.parse(dt).date()
    cha=7-dt1.isoweekday()
    offset=datetime.timedelta(days=cha)
    dt2 = dt1+offset
    i=0
    for i in range(365//7):
        dt3=(dt2+ datetime.timedelta(days = 7*i)).strftime("%Y-%m-%d")
        i=i+1
        print(dt3)
        
year=input('请输入年份:')
all_sundays(year)

你可能感兴趣的:(python基础——task8打卡)