【问题标题】:Erlang how to check if all elements in a list of tuples exists in another list of tuplesErlang如何检查一个元组列表中的所有元素是否存在于另一个元组列表中
【发布时间】:2022-01-08 00:15:57
【问题描述】:

假设我有两个列表: AmountOfProducts 有一个像这样的元组列表

[{apple, 10}, {chocolate, 13}, {lettuce, 9}, {mango, 20}]

第二个列表是 OrderProducts,它有像这样的元组

[{apple, 3}, {chocolate, 1}, {mango 4}]

第一个元素是产品名称的原子,第二个元素是数量,对于 AmountOfProducts,数量是可用的数量,对于 OrderProducts,它是它要求的数量。 有没有办法检查 OrderProducts 的所有原子是否存在于 AmountOfProducts 中并返回 true?如果,假设 OrderProducts 有一个额外的元组,并且它的原子在 AmountOfProducts 中不存在,它会返回 false?这个想法是这样的:

AmountOfProducts = [{apple, 10}, {chocolate, 13}, {lettuce, 9}, {mango, 20}]
OrderProducts = [{apple, 3}, {chocolate, 1}, {mango 4}]

check_products(AmountOfProducts, OrderProducts) ->
    if
        all atoms inside OrderProducts exists in AmountOfProducts -> true;
        one or many of the atoms inside OrderProducts doesnt exist in AmountOfProducts -> false
    end.

在两个列表的给定情况下,它应该返回为真。有没有办法做到这一点?我希望这是有道理的,谢谢!

【问题讨论】:

    标签: list tuples erlang


    【解决方案1】:

    我将向您展示解决此问题的几种方法。

    第一版:列表理解

    check_products(AmountOfProducts, OrderProducts) ->
      [] ==
        [Key || {Key, _} <- OrderProducts
              , not lists:keymember(Key, 1, AmountOfProducts)].
    

    这里我们基本上收集了OrderProducts 中所有在AmountOfProducts 中没有对应元组的键,如果这是一个空列表……我们返回true

    第二版:高阶函数

    check_products(AmountOfProducts, OrderProducts) ->
      lists:all(
        fun({Key, _}) ->
          lists:keymember(Key, 1, AmountOfProducts)
        end, OrderProducts).
    

    这里我们基本上写了你在你的问题中写的内容,但是在 Erlang 代码中:如果 所有的键 OrderProduct 中的元素也是 AmountOfProducts 中的键,则返回 true

    第三版:只比较按键

    现在,如果您知道任何列表中都不会有重复的键,您也可以这样编写代码……

    check_products(AmountOfProducts, OrderProducts) ->
      [] == keys(OrderProducts) -- keys(AmountOfProducts).
    

    基本上说,如果我们从OrderProducts 的键列表中删除所有来自AmountOfProducts 的键...我们最终没有键(它们都被删除,因为它们属于AmountOfProducts)。 你必须实现一个辅助函数keys/1,但希望那个更容易实现(或者你可以使用proplists:get_keys/1

    【讨论】:

    • 为了在较长的项目列表上执行此操作时可以很好地扩展,最好在两个列表上都使用ordsets:from_list(proplists:get_keys(List)),然后使用ordsets:subtract(Set1, Set2) 而不是--,并检查是否结果是 [] 和以前一样。这避免了您将获得的二次时间。
    • 很好的解决方案!
    猜你喜欢
    • 2022-01-06
    • 2016-03-31
    • 2014-09-17
    • 2021-08-24
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多