力扣: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天。

代码:

python 复制代码
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]]})
相关推荐
秋夫人22 分钟前
B+树(B+TREE)索引
数据结构·算法
鸽芷咕39 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
子午1 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期1 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
Milo_K1 小时前
今日 leetCode 15.三数之和
算法·leetcode
Darling_001 小时前
LeetCode_sql_day28(1767.寻找没有被执行的任务对)
sql·算法·leetcode
AlexMercer10121 小时前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
Greyplayground1 小时前
【算法基础实验】图论-BellmanFord最短路径
算法·图论·最短路径
Adolf_19931 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask