力扣:197. 上升的温度(Python3)

题目:

表: Weather

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id 是该表具有唯一值的列。
该表包含特定日期的温度信息

编写解决方案,找出与之前(昨天的)日期相比温度更高的所有日期的 id 。

返回结果 无顺序要求 。

结果格式如下例子所示。

来源:力扣(LeetCode)
链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

示例:

示例 1:

输入:

Weather 表:
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1  | 2015-01-01 | 10          |
| 2  | 2015-01-02 | 25          |
| 3  | 2015-01-03 | 20          |
| 4  | 2015-01-04 | 30          |
+----+------------+-------------+


输出:

+----+
| id |
+----+
| 2  |
| 4  |
+----+


解释:

2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(20 -> 30)

解法:

先根据日期排序,接着比对后一天是不是和当前间隔1天且温度更高。

知识点:

1.pd.Timedelta(value, unit=None, **kwargs)表示两个datetime值之间的差。value:日期形式字符串;unit:指定value的类型。创建方式比如:

pd.Timedelta(days=1)

表示间隔1天。

代码:

import pandas as pd

def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
    weather.sort_values('recordDate', inplace=True)
    w = list(zip(weather['id'], weather['recordDate'], weather['temperature']))
    return pd.DataFrame({'id': [w[index][0] for index in range(1, len(w)) if w[index][1] - w[index - 1][1] == pd.Timedelta(days=1) and w[index][2] > w[index - 1][2]]})

相关推荐

  1. 197. 上升温度Python3

    2023-12-10 05:36:01       38 阅读
  2. 197题:上升温度

    2023-12-10 05:36:01       6 阅读
  3. 196. 删除重复电子邮箱(Python3

    2023-12-10 05:36:01       44 阅读
  4. 191. 位1个数(Python3

    2023-12-10 05:36:01       42 阅读
  5. 198. 打家劫舍(Python3

    2023-12-10 05:36:01       41 阅读
  6. 【LeetCode题库】197. 上升温度 —— 连接查询

    2023-12-10 05:36:01       14 阅读
  7. 每日练习(3.19)补

    2023-12-10 05:36:01       18 阅读
  8. :200. 岛屿数量(Python3

    2023-12-10 05:36:01       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-10 05:36:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-10 05:36:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 05:36:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 05:36:01       18 阅读

热门阅读

  1. 网络规划的组成

    2023-12-10 05:36:01       29 阅读
  2. [LeetCode] 15. 三数之和

    2023-12-10 05:36:01       42 阅读
  3. 如何安装和使用three.js

    2023-12-10 05:36:01       31 阅读
  4. Git:版本控制的艺术与实践

    2023-12-10 05:36:01       40 阅读
  5. RUST博客帖子编辑示例

    2023-12-10 05:36:01       32 阅读
  6. MySQL库与表的备份

    2023-12-10 05:36:01       41 阅读
  7. HJ94 记票统计

    2023-12-10 05:36:01       43 阅读
  8. nvue页面用法uniapp

    2023-12-10 05:36:01       42 阅读
  9. Qt OpenCV 学习(文章链接汇总)

    2023-12-10 05:36:01       45 阅读
  10. 谈一谈Linux下的进程和线程

    2023-12-10 05:36:01       41 阅读
  11. Linux 如何解决磁盘空间没有扩大的问题。

    2023-12-10 05:36:01       32 阅读