您可以使用pl.Expr.list.eval
来判断列表列中的每个元素,无论它是否包含在target
中.然后,布尔列表的和给出匹配的数量.
dst.with_columns(
pl.col("c2").list.eval(pl.element().is_in(targets)).list.sum().alias("res")
)
shape: (4, 4)
┌─────┬─────────────┬─────────────┬─────┐
│ c1 ┆ c2 ┆ match_count ┆ res │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ list[i64] ┆ i64 ┆ u32 │
╞═════╪═════════════╪═════════════╪═════╡
│ a ┆ [0] ┆ 0 ┆ 0 │
│ b ┆ [2, 3, 4] ┆ 1 ┆ 1 │
│ c ┆ [3, 4, … 9] ┆ 3 ┆ 3 │
│ d ┆ [3, 9] ┆ 2 ┆ 2 │
└─────┴─────────────┴─────────────┴─────┘
类似地,您可以使用pl.Expr.list.eval
来过滤列c2
,然后使用pl.Expr.list.len
来查看还剩多少个值.如果这对性能至关重要,我建议对这两种方法进行基准测试.
或者,您确实对集合交集中的元素数量感兴趣(如@jqurious提到的),或者至少一个列表中的元素是唯一的,您可以使用pl.Expr.list.set_intersection
.
dst.with_columns(
pl.col("c2")
.list.set_intersection(targets.to_list())
.list.len()
.alias("res_intersection")
)
shape: (4, 4)
┌─────┬─────────────┬─────────────┬──────────────────┐
│ c1 ┆ c2 ┆ match_count ┆ res_intersection │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ list[i64] ┆ i64 ┆ u32 │
╞═════╪═════════════╪═════════════╪══════════════════╡
│ a ┆ [0] ┆ 0 ┆ 0 │
│ b ┆ [2, 3, 4] ┆ 1 ┆ 1 │
│ c ┆ [3, 4, … 9] ┆ 3 ┆ 3 │
│ d ┆ [3, 9] ┆ 2 ┆ 2 │
└─────┴─────────────┴─────────────┴──────────────────┘