df-drills pandas · Polars · DuckDB SQL · DuckDB Relational 题目列表 登录

列表推导与 lambda

中等 24-duckdb-sql-only DuckDB SQL 专属 listlambdalist-comprehensionlist-reduce

对每篇文章的标签列表 tags 做两步处理:

  1. tags_upper:只保留长度大于 2 个字符的标签,并转成大写。保持原来的顺序,不去重;列表里为 null 的元素丢掉。
  2. tag_line:把 tags_upper 的元素依次用 | 连接成一个字符串,例如 ["ETL", "PYTHON"] → ETL|PYTHON,开头和结尾都没有 |。

null 与空列表:

  • tags 为 null 时,两列都为 null;
  • tags_upper 为空列表(原列表为空,或者没有标签满足条件)时,tag_line 为 null。

考点:第 1 步用“列表推导”写成一个表达式;第 2 步用“对列表做归约(reduce)”的 lambda 写法,lambda 使用 lambda x: ... 语法。判题只看结果。

输入

  • articles(article_id, title, author, tags, published_at, word_count),tags 是字符串列表

输出

列为 article_id, tags_upper, tag_line,每篇文章一行,行顺序不限。

输入表 (case base,共 2 个 case:base、edge)

articles blog/articles
#article_idint64titlestringauthorstruct<name: string, country: string>tagslist<string>published_atdatetimeword_countint64
1 1 A gentle intro to DuckDB {"name": "Hans Muller", "country": "DE"} ["etl", "Python", "rust"] 2026-02-07 18:20:00 353
2 2 Testing Python {"name": "Bob Smith", "country": "US"} ["SQL"] 2026-09-02 17:13:00 3726
3 3 10 tips for dataframes {"name": "Bob Smith", "country": "US"} ["pandas"] 2026-07-23 20:39:00 362
4 4 Debugging DuckDB {"name": "Amy Chen", "country": "US"} [] 2026-01-20 22:03:00 3845
5 5 Debugging window functions {"name": "Ana Souza", "country": "BR"} [] 2026-08-20 10:17:00 3899
6 6 Getting started with Polars {"name": "王芳", "country": "CN"} ["polars"] 2026-05-17 19:28:00 2086
7 7 Testing dataframes {"name": "Ana Souza", "country": "BR"} ["rust", "ml", "go"] 2026-08-01 17:58:00 1251
8 8 Benchmarking pandas {"name": "王芳", "country": "CN"} ["Python", "pandas", "ml", "go"] 2026-02-27 20:10:00 2984
9 9 Benchmarking Python {"name": "Hans Muller", "country": "DE"} ["etl"] 2026-09-09 09:30:00 1709
10 10 10 tips for dataframes {"name": "Amy Chen", "country": "US"} null 2026-08-11 21:07:00 2000
11 11 Debugging pandas {"name": "Amy Chen", "country": "US"} ["python", "viz"] 2026-06-23 17:24:00 3908
12 12 Getting started with SQL {"name": "Ana Souza", "country": "BR"} ["sql"] 2026-03-01 18:14:00 2493
13 13 Common pitfalls in SQL {"name": "Li Wei", "country": "CN"} ["python", "pandas", "python"] 2026-03-25 19:46:00 473
14 14 A gentle intro to ETL {"name": "王芳", "country": "CN"} ["viz", "SQL", "etl", "go"] 2026-07-21 18:59:00 3850
15 15 Common pitfalls in Python {"name": "Amy Chen", "country": "US"} ["SQL", "polars"] 2026-06-08 15:27:00 1357
16 16 Getting started with ETL {"name": "Ana Souza", "country": "BR"} ["rust", "ml", "python"] 2026-07-16 18:30:00 2713
17 17 A gentle intro to pandas {"name": "Kenji Sato", "country": "JP"} ["Python", "ml"] 2026-05-27 20:35:00 2305
18 18 Getting started with pandas {"name": "Li Wei", "country": "CN"} null 2026-06-17 11:01:00 1790
19 19 A gentle intro to ETL {"name": "Kenji Sato", "country": "JP"} ["duckdb", "SQL", "viz"] 2026-02-27 21:10:00 3630
20 20 Benchmarking DuckDB {"name": "Li Wei", "country": "CN"} ["Python", "duckdb"] 2026-05-07 13:35:00 1067
显示前 20 行,共 40 行

期望输出 (case base,行顺序不限)

#article_idInt64tags_upperList(String)tag_lineString
1 1 ["ETL", "PYTHON", "RUST"] ETL|PYTHON|RUST
2 2 ["SQL"] SQL
3 3 ["PANDAS"] PANDAS
4 4 [] null
5 5 [] null
6 6 ["POLARS"] POLARS
7 7 ["RUST"] RUST
8 8 ["PYTHON", "PANDAS"] PYTHON|PANDAS
9 9 ["ETL"] ETL
10 10 null null
11 11 ["PYTHON", "VIZ"] PYTHON|VIZ
12 12 ["SQL"] SQL
13 13 ["PYTHON", "PANDAS", "PYTHON"] PYTHON|PANDAS|PYTHON
14 14 ["VIZ", "SQL", "ETL"] VIZ|SQL|ETL
15 15 ["SQL", "POLARS"] SQL|POLARS
16 16 ["RUST", "PYTHON"] RUST|PYTHON
17 17 ["PYTHON"] PYTHON
18 18 null null
19 19 ["DUCKDB", "SQL", "VIZ"] DUCKDB|SQL|VIZ
20 20 ["PYTHON", "DUCKDB"] PYTHON|DUCKDB
21 21 ["PYTHON"] PYTHON
22 22 ["POLARS", "RUST", "ETL"] POLARS|RUST|ETL
23 23 ["SQL", "DUCKDB", "SQL", "SQL"] SQL|DUCKDB|SQL|SQL
24 24 ["DUCKDB"] DUCKDB
25 25 ["PYTHON", "VIZ"] PYTHON|VIZ
26 26 ["SQL", "POLARS", "PYTHON", "ETL"] SQL|POLARS|PYTHON|ETL
27 27 ["SQL", "POLARS", "PYTHON", "RUST"] SQL|POLARS|PYTHON|RUST
28 28 ["PANDAS", "DUCKDB"] PANDAS|DUCKDB
29 29 ["PANDAS"] PANDAS
30 30 ["SQL"] SQL
31 31 [] null
32 32 ["VIZ", "PYTHON"] VIZ|PYTHON
33 33 [] null
34 34 ["PANDAS"] PANDAS
35 35 ["DUCKDB"] DUCKDB
36 36 [] null
37 37 ["PYTHON"] PYTHON
38 38 ["ETL"] ETL
39 39 ["DUCKDB"] DUCKDB
40 40 ["SQL"] SQL
共 40 行