Подробнее о различии между комбинацией и перестановкой см.: https://en.wikipedia.org/wiki/Combination
Каковы наилучшие способы создания комбинации вариантов (т. е. порядка элементы не должны иметь значения)? Мои текущие решения выглядят следующим образом:
Код: Выделить всё
import polars as pl
choices = pl.DataFrame(
[
pl.Series("flavor", ["x"] * 2 + ["y"] * 3),
pl.Series("choice", ["a", "b"] + ["1", "2", "3"]),
]
)
# join to produce the choices
choices.join(choices, on=["flavor"]).with_columns(
# generate a 2-element list representing the choice
sorted_choice_pair=pl.concat_list("choice", "choice_right").list.sort()
).filter(pl.col.choice.eq(pl.col.sorted_choice_pair.list.first()))
Код: Выделить всё
shape: (9, 4)
┌────────┬────────┬──────────────┬────────────────────┐
│ flavor ┆ choice ┆ choice_right ┆ sorted_choice_pair │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ list[str] │
╞════════╪════════╪══════════════╪════════════════════╡
│ x ┆ a ┆ a ┆ ["a", "a"] │
│ x ┆ a ┆ b ┆ ["a", "b"] │
│ x ┆ b ┆ b ┆ ["b", "b"] │
│ y ┆ 1 ┆ 1 ┆ ["1", "1"] │
│ y ┆ 1 ┆ 2 ┆ ["1", "2"] │
│ y ┆ 2 ┆ 2 ┆ ["2", "2"] │
│ y ┆ 1 ┆ 3 ┆ ["1", "3"] │
│ y ┆ 2 ┆ 3 ┆ ["2", "3"] │
│ y ┆ 3 ┆ 3 ┆ ["3", "3"] │
└────────┴────────┴──────────────┴────────────────────┘
Подробнее здесь: https://stackoverflow.com/questions/792 ... rmutations
Мобильная версия