【问题标题】:Need assistance optimizing `each` iteration需要帮助优化“每个”迭代
【发布时间】:2015-11-20 23:38:24
【问题描述】:

我有@acct_list

{"account_id"=>1, "customer_id"=>1, "balance"=>0.0, 0=>1, 1=>1, 2=>0.0}
{"account_id"=>2, "customer_id"=>1, "balance"=>1000.0, 0=>2, 1=>nil, 2=>1000.0}
{"account_id"=>3, "customer_id"=>2, "balance"=>9.0, 0=>3, 1=>2, 2=>9.0}
{"account_id"=>4, "customer_id"=>2, "balance"=>0.0, 0=>4, 1=>2, 2=>0.0}

我正在尝试显示银行计划的客户 # 和帐户 #。我希望我的显示器显示如下内容:

Customer #1 Account #1  -----  Balance: $0.0

            Account #2  -----  Balance: $1000.0

Customer #2 Account #3  -----  Balance: $9.0

            Account #4  -----  Balance: $0.0

每个客户应该只显示一次,以及该客户拥有的帐户总数。

这是我的代码的样子:

def self.account_info2(acct_list)
  account_id_array = []
  acct_list.each do |a|
    print "\n"
    print 'Customer #' + a["customer_id"].to_s + ' Account #' + a["account_id"].to_s + "  -----  " + "Balance: $" +
    a["balance"].to_s
    print "\n"
    account_id_array.push(a["account_id"])
  end
  account_id_array
end
@acct_list = bank.account_list(@man_name, @man_pin)
return account_info2(@acct_list)

它会显示不必要的 Customer # 重复:

Customer #1 Account #1  -----  Balance: $0.0

Customer #1 Account #2  -----  Balance: $1000.0

Customer #2 Account #3  -----  Balance: $9.0

Customer #2 Account #4  -----  Balance: $0.0

【问题讨论】:

  • 您想要的输出具有误导性!它将帐户 2 显示为属于客户 1,而实际上它并不属于他。
  • 出了点问题。您的输出或示例数据。
  • 对不起。是一个错字。它确实属于。简单的错误。修正错误。
  • 我建议您编辑您的问题以进行一些更改。 1.将最后四行表示为(命名的)数组,如arr = [{"account_id"=>1,...,2=>0.0},...'。 2. 将arr 移动到开头并说给你这个数组。 3. 说明数组的元素按客户分组,如果"customer_id" 的值为nil,则它与前一个元素在同一个客户组中。 4.陈述你的问题:你如何打印arr的元素,使它看起来像这样:。
  • 您编辑了问题以完全改变含义。更糟糕的是,你这样做没有告诉读者你有。这样的变化会使答案和 cmets 变得毫无意义。我的回答就是证明。您最初的问题中最重要的方面是 "customer_id" could be nil 的值。你在完全沉默中改变了这一点。我很少对问题投反对票,但在这里我例外。

标签: ruby iteration


【解决方案1】:
list = [
    {"account_id" => 1, "customer_id" => 1, "balance" => 0.0, 0 => 1, 1 => 1, 2 => 0.0},
    {"account_id" => 2, "customer_id" => 1, "balance" => 1000.0, 0 => 2, 1 => nil, 2 => 1000.0},
    {"account_id" => 3, "customer_id" => 2, "balance" => 9.0, 0 => 3, 1 => 2, 2 => 9.0},
    {"account_id" => 4, "customer_id" => 2, "balance" => 0.0, 0 => 4, 1 => 2, 2 => 0.0}
]

list.chunk{|h| h["customer_id"]}.each { |customer_id, customer_data|
    msg = "Customer # #{customer_id}"
    cust_id_filter = ' ' * (msg.size)
    customer_data.each { |h|
        print (msg ? msg : cust_id_filter) + " Account # #{h["account_id"]} ---- Balance: $ #{h["balance"]}\n\n"
        msg = nil if msg
    }
}

你应该输出:

Customer # 1 Account # 1 ---- Balance: $ 0.0

             Account # 2 ---- Balance: $ 1000.0

Customer # 2 Account # 3 ---- Balance: $ 9.0

             Account # 4 ---- Balance: $ 0.0

【讨论】:

    【解决方案2】:

    编辑:我的解决方案回答了原始问题。如果您有兴趣知道那是什么,请查看问题的修订历史记录。

    您的(修改后的)哈希数组是:

    arr = [{ "account_id"=>1, "customer_id"=>1,   "balance"=>0.0 },
           { "account_id"=>2, "customer_id"=>nil, "balance"=>1000.0 },
           { "account_id"=>3, "customer_id"=>1,   "balance"=>500.0 },
           { "account_id"=>4, "customer_id"=>2,   "balance"=>9.0 },
           { "account_id"=>5, "customer_id"=>2,   "balance"=>0.0 }]
    

    这是以所需格式打印arr 内容的一种简单方法:

    CUST_ID_SIZE = 1
    CUST_ID_FILLER = ' '*("Customer # ".size + CUST_ID_SIZE)
      #=> "            "
    
    last_id = nil
    arr.each do |h|
      if h["customer_id"] && h["customer_id"] != last_id
        last_id = h["customer_id"]
        print "Customer # #{ h["customer_id"] }"
      else
        print CUST_ID_FILLER
      end
        puts " Account # #{ h["account_id"] } -----  Balance: $ #{ h["balance"] }\n\n"
    end
    Customer # 1 Account # 1 -----  Balance: $ 0.0
    
                 Account # 2 -----  Balance: $ 1000.0
    
                 Account # 3 -----  Balance: $ 500.0
    
    Customer # 2 Account # 4 -----  Balance: $ 9.0
    
                 Account # 5 -----  Balance: $ 0.0
    

    【讨论】:

    • 是的,这可能可以简化一点:)
    • 谢谢@Sergio。我决定用砍刀。
    【解决方案3】:

    似乎group_by 会在这里为您提供帮助。您有一堆需要按customer_id 分组的帐户数据,然后进行处理以供输出。

    group_by 返回一个哈希值,其中键是分组项(customer_id's),值是输入数组。您实际上并不需要密钥,因此请在结果上调用 .values 以使 customer_accounts 成为一个数组数组,其中每个数组元素包含与一个客户关联的每个帐户的哈希数组。像这样:

    [[{"account_id"=>1, "customer_id"=>1, "balance"=>0.0}, {"account_id"=>2, "customer_id"=>1, "balance"=>1000.0}], [{"account_id"=>3, "customer_id"=>2, "balance"=>9.0}, {"account_id"=>4, "customer_id"=>2, "balance"=>0.0}]]

    然后遍历customer_accounts 以获取每个客户的信息并使用with_index 选项遍历每个customer,以便您可以将Customer # 仅分配给第一个,并将所需的间距分配给所有其他人,收集所有结果都放入account_output 字符串中。

    account_list = [{"account_id"=>1, "customer_id"=>1, "balance"=>0.0}, 
    {"account_id"=>2, "customer_id"=>1, "balance"=>1000.0}, 
    {"account_id"=>3, "customer_id"=>2, "balance"=>9.0}, 
    {"account_id"=>4, "customer_id"=>2, "balance"=>0.0}]
    
    def account_info(acct_list)
      account_output = ""
    
      customer_accounts = acct_list.group_by { |account| account['customer_id'] }.values
    
      customer_accounts.each do |customer|
        customer.each_with_index do |account, index|
          if index == 0
            account_output += "Customer ##{account['customer_id']} Account ##{account['account_id']} ----- Balance: $#{account['balance']}\n"
          else
            account_output += "            Account ##{account['account_id']} ----- Balance: $#{account['balance']}\n"
          end
        end
      end
    
      print account_output
    
    end
    
    account_info(account_list)
    

    【讨论】:

      【解决方案4】:
      @acct_list = [
        {"account_id"=>1, "customer_id"=>1, "balance"=>0.0, 0=>1, 1=>1, 2=>0.0},
        {"account_id"=>2, "customer_id"=>1, "balance"=>1000.0, 0=>2, 1=>nil, 2=>1000.0},
        {"account_id"=>3, "customer_id"=>2, "balance"=>9.0, 0=>3, 1=>2, 2=>9.0},
        {"account_id"=>4, "customer_id"=>2, "balance"=>0.0, 0=>4, 1=>2, 2=>0.0}
      ]
      
      @acct_list.chunk{|h| h["customer_id"]}
      .each do |customer, a| a
        .each.with_index do |h, i|
          puts \
            (("Customer ##{customer}" if i.zero?)).to_s.ljust(11) +
            "Account ##{h["account_id"]}  -----  Balance: $#{h["balance"]}",
            nil
        end
      end
      

      输出:

      Customer #1 Account #1  -----  Balance: $0.0
      
                  Account #2  -----  Balance: $1000.0
      
      Customer #2 Account #3  -----  Balance: $9.0
      
                  Account #4  -----  Balance: $0.0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-27
        • 1970-01-01
        • 1970-01-01
        • 2014-12-23
        相关资源
        最近更新 更多