根据以前的购物旅行对购物清单进行排序

发布于 2025-02-07 05:49:24 字数 394 浏览 0 评论 0原文

我想根据以前的购物旅行中检查订单项目的购物清单。例如,我去商店和购物苹果,香蕉和鸡蛋。

接下来,我去商店我买鳄梨,番茄和苹果。在我下次旅行中,应用程序将鳄梨,番茄和苹果分类在鸡蛋fe之前,

我发现了这篇文章谈论拓扑排序:如何根据以前的选订单对购物清单进行分类?

但是,我不确定这是如何工作的,因为从理论上讲,我可以有周期(用户可以从理论上检查苹果,然后是香蕉,然后在苹果面前检查香蕉)。

您能指导我解决这个问题吗?

亲切的问候

I want to sort a shopping list based on the order items were checked off in previous shopping trips. For example I go to the store and shop Apples, Bananas and Eggs.

Next I go to the store I shop Avocados and Tomatos and Apples. For my next trip the application sorts Avocados, Tomatos and Apples all before Eggs f.e.

I found this post talking about topological sorting: How to sort a shopping list based on previous pick order?.

However I am not sure how this should work since in theory I could have cycles (A user could theoretically check off apples and then bananas and the next time bananas are checked off before apples).

Could you guide me on how to tackle this problem?

Kind regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

束缚m 2025-02-14 05:49:24

我假设:

  1. 过去的物品订购应指导订购当前订单。
  2. 在历史上订购的任何物品后,任何新项目都会出现。
  3. 与最近的订单相比,订购时间更高的时间应具有较小的影响。

我的想法是根据以下订单中看到的物品分配权重:

  1. 它们在历史秩序中的地位。
  2. 那个气味多大了。

权重可能需要调整,但是,使用您链接到的其他问题的数据,下面的 python代码确实会根据历史订单创建订单:

from collections import defaultdict

shop = [['Toothpaste', 'Bread', 'Meat', 'Vegetables', 'Milk', 'Ice cream'],      # last
        ['CDs', 'Bread', 'Fruit', 'Vegetables', 'Juice', 'Sugar', 'Chocolates'], # last-but-1
        ['Meat', 'Juice', 'Milk', 'Sugar']]  # last-but-2

def weight_from_index(idx: int) -> float | int:
    "Items to the left are of LOwer wt and will sort first."
    return idx + 1

def historic_multiplier(idy: int) -> float:
    "Older rows have larger multipliers and so are of lower overall weight."
    return (idy + 1)**1

def shopping_weights(history: list[list[str]]) -> dict[str, int | float]:
    "Weight for items from historic shops."
    item2weight = defaultdict(float)
    for y, hist in enumerate(history):
        for x, item in enumerate(hist):
            item2weight[item] += historic_multiplier(y) * weight_from_index(x)
    return dict(item2weight)

def order_items(items: list[str], weights) -> list[str]:
    wts = weights.copy()
    new_items = set(items) - set(wts)

    # New items last, but in given order otherwise
    max_wt = max(wts.values())
    for itm in new_items:
        wts[itm] = max_wt + 1 + items.index(itm)

    return sorted(items, key = lambda i: wts[i])


item_weights = shopping_weights(shop)
new_shop = ['Curry', 'Vegetables', 'Eggs', 'Milk', 'CDs', 'Meat']

new_order = order_items(new_shop, item_weights)
print(new_order)
# ['CDs', 'Meat', 'Vegetables', 'Milk', 'Curry', 'Eggs']


# Update the historic item orders
shop.insert(0, new_order)

I assume:

  1. Past item orderings should guide ordering the current order.
  2. Any new items appear after any items ordered historically.
  3. Orderings further back in time should have less impact than more recent orderings.

My idea is to assign weights to items seen in past orders based on:

  1. Their position in a historic ordering.
  2. How old that odering is.

The weightings might need adjusting, but, using data from that other question you link to, the Python code below does create orderings based on historic orderings:

from collections import defaultdict

shop = [['Toothpaste', 'Bread', 'Meat', 'Vegetables', 'Milk', 'Ice cream'],      # last
        ['CDs', 'Bread', 'Fruit', 'Vegetables', 'Juice', 'Sugar', 'Chocolates'], # last-but-1
        ['Meat', 'Juice', 'Milk', 'Sugar']]  # last-but-2

def weight_from_index(idx: int) -> float | int:
    "Items to the left are of LOwer wt and will sort first."
    return idx + 1

def historic_multiplier(idy: int) -> float:
    "Older rows have larger multipliers and so are of lower overall weight."
    return (idy + 1)**1

def shopping_weights(history: list[list[str]]) -> dict[str, int | float]:
    "Weight for items from historic shops."
    item2weight = defaultdict(float)
    for y, hist in enumerate(history):
        for x, item in enumerate(hist):
            item2weight[item] += historic_multiplier(y) * weight_from_index(x)
    return dict(item2weight)

def order_items(items: list[str], weights) -> list[str]:
    wts = weights.copy()
    new_items = set(items) - set(wts)

    # New items last, but in given order otherwise
    max_wt = max(wts.values())
    for itm in new_items:
        wts[itm] = max_wt + 1 + items.index(itm)

    return sorted(items, key = lambda i: wts[i])


item_weights = shopping_weights(shop)
new_shop = ['Curry', 'Vegetables', 'Eggs', 'Milk', 'CDs', 'Meat']

new_order = order_items(new_shop, item_weights)
print(new_order)
# ['CDs', 'Meat', 'Vegetables', 'Milk', 'Curry', 'Eggs']


# Update the historic item orders
shop.insert(0, new_order)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文