Day3打卡,列表、循环与判断语句

@浙大疏锦行

常用的列表函数
list_ex = [1,2,3]
# 添加元素,append在末尾添加单个元素
list_ex.append(4)
print(list_ex)
list_ex.extend([5,6,7])
# extend在末尾添加多个元素
print(list_ex)
list_ex.insert(1,8)
# 在索引处插入元素
print(list_ex)
# 删除元素
list_ex.remove(6)
# remove删除第一个指定元素
print(list_ex)
list_ex.pop(3)
# pop删除指定索引元素,并返回删除的元素
print(list_ex)
list_ex.clear()
# clear清空列表
print(list_ex)


[1, 2, 3, 4]
[1, 2, 3, 4, 5, 6, 7]
[1, 8, 2, 3, 4, 5, 6, 7]
[1, 8, 2, 3, 4, 5, 7]
[1, 8, 2, 4, 5, 7]
[]

题目1:列表的基础操作

题目:

  1. 创建一个包含三个字符串元素的列表 tech_list,元素分别为 “Python”, “Java”, “Go”。
  2. 获取列表中的第一个元素,并将其存储在变量 first_tech 中。
  3. 向 tech_list 的末尾添加一个新的字符串元素 “JavaScript”。
  4. 修改 tech_list 中的第二个元素(索引为 1),将其从 “Java” 更改为 “Ruby”。
  5. 移除列表中的元素 “Go”。
  6. 计算当前 tech_list 的长度,并将结果存储在变量 current_length 中。
  7. 最后,使用 f-string 分三行打印出以下信息:
    1. 获取到的第一个技术名称。
    2. 列表当前的长度。
    3. 经过所有操作后,列表最终的内容。

打印格式应类似:

第一个技术是: Python

当前列表长度: 3

最终列表内容: [‘Python’, ‘Ruby’, ‘JavaScript’]

tech_list = ["Python","Java","Go"]
first_tech = tech_list[0]
tech_list.append("JavaScript")
tech_list[1] = "Ruby"
# 列表中更改元素,直接重新赋值即可
tech_list.remove("Go")
current_length = len(tech_list)
print(f"获取到的第一个技术是:{first_tech}")
print(f"列表当前长度为:{current_length}")
print(f"最终的列表内容为:{tech_list}")

获取到的第一个技术是:Python
列表当前长度为:3
最终的列表内容为:['Python', 'Ruby', 'JavaScript']

题目2:循环for语句

计算1+100的和 用for循环来写

常见的循环用法

# 1. 遍历列表
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# 2. 使用range()生成数字序列
for i in range(5):  # 生成0-4的整数
    print(i)

# 3. 遍历字符串(按字符)
for char in "Python":
    print(char)

# 4. 带索引遍历(enumerate)
for index, value in enumerate(fruits):
    print(f"索引{index}: {value}")

解题

sum_result = 0
# 左闭右开
for i in range(1,101):
    sum_result += i
print(f"1-100的和为{sum_result}")

1-100的和为5050

题目3:判断语句

温度预警系统

1. 定义一个变量temperature存储当前温度(整数)

2. 根据以下条件判断并打印预警信息:

   - 高于35度:打印"红色预警:高温天气!"

   - 28-35度:打印"黄色预警:天气炎热"

   - 20-27度:打印"绿色提示:适宜温度"

   - 低于20度:打印"蓝色预警:注意保暖"

3. 使用if-elif-else结构实现

4. 测试用例:用38你的代码

temperature = int(input("请输入温度:"))
print(f"您输入的温度为:{temperature}")
if temperature > 35:
    print("红色预警:高温天气!")
elif temperature > 28 and temperature<= 35:
    print("黄色预警:天气炎热")
elif temperature >= 20 and temperature <= 28:
    print("绿色提示:适宜温度")
elif temperature <20:
    print("蓝色预警:注意保暖")
else:
    print("输入错误")


您输入的温度为:38
红色预警:高温天气!

题目4

  1. 定义一个包含整数的列表 scores,赋值为 [85, 92, 78, 65, 95, 88]。
  2. 初始化两个变量:excellent_count 用于记录分数大于等于 90 的个数,初始值为 0;total_score 用于累加所有分数,初始值为 0。
  3. 使用 for 循环遍历 scores 列表中的每一个分数。
  4. 在循环内部:
    1. 将当前分数累加到 total_score 变量上。
    2. 使用 if 语句判断当前分数是否大于等于 90。如果是,则将 excellent_count 变量加 1。
  5. 循环结束后,计算平均分 average_score(总分除以分数的个数)。
  6. 使用 f-string 分三行打印出以下信息:
  7. 优秀分数(>=90)的个数。
  8. 所有分数的总和。
  9. 所有分数的平均分(结果包含3位小数)。

打印格式应类似:

优秀分数个数: 3

分数总和: 503

平均分数: 83.833

score = [85,92,78,65,95,88]
excellent_count = 0
total_score = 0
for i in score:
    total_score += i
    if i >= 90:
        excellent_count += 1
average_score = total_score/len(score)
print(f"优秀分数个数:{excellent_count}")
print(f"分数总和:{total_score}")
print(f"平均分数:{average_score:.3f}")

优秀分数个数:2
分数总和:503
平均分数:83.833

import pandas as pd from datetime import datetime, timedelta # 假设公休日数据已作为内部变量存在 (示例格式) rest_days_data = [ { "员工": "林爱梅", "员工邮箱": "linaimei@silanic.cn", "工号": "96200288", "组织": "大客户部", "工作制": "排班制", "排班明细": { "2025-08-01": "常白班-不定时", "2025-08-02": "day-公休日(8:30-17:30)", # ... (其他日期数据) "2025-08-31": "day-公休日(8:30-17:30)" } } ] def convert_travel_to_clock(travel_file, output_file): """ 将出差数据转换为打卡记录 :param travel_file: 输入出差数据Excel文件路径 :param output_file: 输出打卡数据Excel文件路径 """ # 1. 构建公休日字典 {工号: {日期字符串: True}} rest_dict = {} for emp_data in rest_days_data: emp_id = str(emp_data["工号"]) rest_dict[emp_id] = {} for date_str, shift in emp_data["排班明细"].items(): if "公休日" in shift: rest_dict[emp_id][date_str] = True # 2. 读取出差数据 travel_df = pd.read_excel(travel_file) clock_records = [] # 3. 处理每条出差记录 for _, row in travel_df.iterrows(): emp_id = str(row["工号"]) start_time = row["开始时间"] end_time = row["结束时间"] # 确保时间类型正确 if not isinstance(start_time, datetime): start_time = pd.to_datetime(start_time) if not isinstance(end_time, datetime): end_time = pd.to_datetime(end_time) start_date = start_time.date() end_date = end_time.date() # 情况1: 当天出差 (开始日期=结束日期) if start_date == end_date: # 规则1: 结束时间为17:30 → 补开始时间和17:30 if end_time.time() == datetime.strptime("17:30", "%H:%M").time(): clock_records.append({ "员工": row["员工"], "员工邮箱": row["员工邮箱"], "刷卡时间": start_time, "打卡状态": "进" }) clock_records.append({ "员工": row["员工"], "员工邮箱": row["员工邮箱"], "刷卡时间": end_time.replace(hour=17, minute=30, second=0), "打卡状态": "出" }) # 规则4: 上午出差 (8:30~11:30) → 补上班卡 elif (start_time.time() >= datetime.strptime("08:30", "%H:%M").time() and end_time.time() <= datetime.strptime("11:30", "%H:%M").time()): clock_records.append({ "员工": row["员工"], "员工邮箱": row["员工邮箱"], "刷卡时间": start_time.replace(hour=8, minute=30, second=0), "打卡状态": "进" }) # 规则3: 中间时段出差 → 不补卡 # 情况2: 跨天出差 else: current_date = start_date # 生成日期范围 (从开始日期到结束日期) while current_date <= end_date: date_str = current_date.strftime("%Y-%m-%d") # 跳过公休日 if emp_id in rest_dict and date_str in rest_dict[emp_id]: current_date += timedelta(days=1) continue # 规则2: 所有工作日补8:30进和17:30出 clock_in = datetime.combine(current_date, datetime.strptime("08:30", "%H:%M").time()) clock_out = datetime.combine(current_date, datetime.strptime("17:30", "%H:%M").time()) clock_records.append({ "员工": row["员工"], "员工邮箱": row["员工邮箱"], "刷卡时间": clock_in, "打卡状态": "进" }) clock_records.append({ "员工": row["员工"], "员工邮箱": row["员工邮箱"], "刷卡时间": clock_out, "打卡状态": "出" }) current_date += timedelta(days=1) # 4. 创建打卡DataFrame并导出 if clock_records: clock_df = pd.DataFrame(clock_records) # 格式化时间为YYYY/MM/DD HH:MM:SS clock_df["刷卡时间"] = clock_df["刷卡时间"].dt.strftime("%Y/%m/%d %H:%M:%S") clock_df.to_excel(output_file, index=False, columns=["员工", "员工邮箱", "刷卡时间", "打卡状态"], header=["*员工", "*员工邮箱", "*刷卡时间", "打卡状态"]) return f"转换完成,共生成 {len(clock_df)} 条打卡记录" return "未生成任何打卡记录" # 使用示例 if __name__ == "__main__": input_file = "出差数据.xlsx" # 输入文件路径 output_file = "打卡数据.xlsx" # 输出文件路径 result = convert_travel_to_clock(input_file, output_file) print(result) 这个代码中 ,跳过公休日 那段代码 , current_day = timedelta(days=1)什么意思
09-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值