Task08:模块与datetime模块

练习题:

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
from collections import Counter

def most_element(language):
    """ Return a list of lines after inserting a word in a specific line. """
    m=Counter(language)
    for key,value in m.items():
        if(value == max(m.values())):
            return key
    
language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
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
“”"

def to_timestamp(dt_str, tz_str):
# your code here
pass

from datetime import datetime, timedelta, timezone
from dateutil import parser
import re


def to_timestamp(dt_str, tz_str):
    dt = parser.parse(dt_str)
    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 = dt.replace(tzinfo=tz_utc)
    return dt.timestamp()

dt_str1, tz_str1 = '2020-6-1 08:10:30', 'UTC+7:00'
dt_str2, tz_str2 = '2020-5-31 16:10:30', 'UTC-09:00'
print(to_timestamp(dt_str1, tz_str1))
print(to_timestamp(dt_str2, tz_str2))

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

from datetime import date, timedelta

def all_sundays(year):
    year = int(year)
    year_start = date(year, 1, 1)
    # 找出第一个星期天
    year_start += timedelta(days=(7-year_start.isoweekday()))
    while year_start.year == year:
        print(year_start)
        year_start += timedelta(days=7)
        
all_sundays(2020)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中提到,当我们尝试调用一个datetime对象,但这个对象不支持调用操作时,会报出“TypeError: ‘datetime.datetime’ object is not callable”这个错误提示。而引用中给出了一个具体的代码示例,使用了DateTime.ParseExact方法来赋值给变量。但是这段代码并没有解决问题。 如果我们要给datetime.datetime赋值,可以使用datetime模块中的datetime类来创建一个datetime对象,并将需要赋值的日期和时间作为参数传入。例如,可以使用datetime.datetime(2023, 1, 1)来创建一个表示2023年1月1日的datetime对象。这样就可以将这个对象赋值给datetime.datetime变量了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python错误提示:datetime.datetime对象不可调用](https://blog.csdn.net/update7/article/details/129829106)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.](https://blog.csdn.net/Czhenya/article/details/125991332)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值