当前位置: 首页 > news >正文

【Leetcode 30天Pandas挑战】学习记录

这个系列难度比较低,一题写一篇其实没必要,就全部放到一篇吧


题目列表:

  • 595. Big Countries
  • 1757. Recyclable and Low Fat Products

595. Big Countries

原题链接:595. Big Countries

Table: World

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| name        | varchar |
| continent   | varchar |
| area        | int     |
| population  | int     |
| gdp         | bigint  |
+-------------+---------+In SQL, name is the primary key column for this table.
Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value.

A country is big if:

  • it has an area of at least three million (i.e., 3000000 km2), or
  • it has a population of at least twenty-five million (i.e., 25000000).
    Find the name, population, and area of the big countries.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:

World table:
+-------------+-----------+---------+------------+--------------+
| name        | continent | area    | population | gdp          |
+-------------+-----------+---------+------------+--------------+
| Afghanistan | Asia      | 652230  | 25500100   | 20343000000  |
| Albania     | Europe    | 28748   | 2831741    | 12960000000  |
| Algeria     | Africa    | 2381741 | 37100000   | 188681000000 |
| Andorra     | Europe    | 468     | 78115      | 3712000000   |
| Angola      | Africa    | 1246700 | 20609294   | 100990000000 |
+-------------+-----------+---------+------------+--------------+

Output:

+-------------+------------+---------+
| name        | population | area    |
+-------------+------------+---------+
| Afghanistan | 25500100   | 652230  |
| Algeria     | 37100000   | 2381741 |
+-------------+------------+---------+

思路:

两个条件进行筛选,没啥好说的

pandas写法:

import pandas as pddef big_countries(world: pd.DataFrame) -> pd.DataFrame:res = world[(world['area'] >= 3000000) | (world['population'] >= 25000000)]return res[['name', 'population', 'area']]

mysql写法:

select name, population, area 
from World
where population>=25000000 or area>=3000000



1757. Recyclable and Low Fat Products

Table: Products

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_id  | int     |
| low_fats    | enum    |
| recyclable  | enum    |
+-------------+---------+
In SQL, product_id is the primary key for this table.
low_fats is an ENUM of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
recyclable is an ENUM of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.

Find the ids of products that are both low fat and recyclable.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:

Products table:
+-------------+----------+------------+
| product_id  | low_fats | recyclable |
+-------------+----------+------------+
| 0           | Y        | N          |
| 1           | Y        | Y          |
| 2           | N        | Y          |
| 3           | Y        | Y          |
| 4           | N        | N          |
+-------------+----------+------------+

Output:

+-------------+
| product_id  |
+-------------+
| 1           |
| 3           |
+-------------+

Explanation: Only products 1 and 3 are both low fat and recyclable.

思路:

两个条件进行筛选,和上面那个就是and和or的区别,没啥好说的

pandas写法:

import pandas as pddef find_products(products: pd.DataFrame) -> pd.DataFrame:res = products[(products['low_fats'] == 'Y') & (products['recyclable'] == 'Y')]return res[['product_id']]

mysql写法:

select product_id 
from Products
where low_fats = 'Y' and recyclable = 'Y'



http://www.lryc.cn/news/106381.html

相关文章:

  • 微信小程序使用 canvas 2d 实现签字板组件
  • 区块链赋能新时代司法体系,中移链打造可信存证服务
  • ELK报错no handler found for uri and method [PUT] 原因
  • Sublime操作技巧笔记
  • JVM | 基于类加载的一次完全实践
  • Termux实现电脑端远程操作【开启SSH的完整教程】
  • java(Collection类)
  • VS2019编译安装OpenMesh8.0
  • Python爬虫遇到URL错误解决办法大全
  • 基于Vue+ElementUI+Echarts+G2Plot的大屏设计器,代码完全开源
  • Linux - PostgreSQL 适用于9.x 以上的 tar.gz 源码安装与理解 - 报错集锦
  • Django使用用户列表的展示和添加
  • kubernetes错误汇总
  • [openCV]基于拟合中线的智能车巡线方案V4
  • 【网络云盘客户端】——上传文件的功能的实现
  • WebView2对比CefSharp的超强优势
  • 前端需要知道的计算机网络知识
  • [2023杭电多校5 1005] Snake (生成函数)
  • 【MyBtis】各种查询功能
  • H5打包封装小程序系统开发
  • SpringBoot集成jasypt,加密yml配置文件
  • 【C++】模板(初阶)
  • windows下的txt文档,传到ubuntu后,每行后面出现^M,怎么处理?
  • LabVIEW FPGA开发实时滑动摩擦系统
  • Prometheus服务器、Prometheus被监控端、Grafana、Prometheus服务器、Prometheus被监控端、Grafana
  • 常见的锁策略(面试八股文)
  • SO_KEEPALIVE、TCP_KEEPIDLE、TCP_KEEPINTVL、保活包
  • 【phaser微信抖音小游戏开发005】画布上添加图片
  • 【设计模式——学习笔记】23种设计模式——外观模式Facade(原理讲解+应用场景介绍+案例介绍+Java代码实现)
  • 消息队列 -提供上层服务接口