Remove duplicates from loop

后端 未结 3 1140
醉话见心
醉话见心 2021-01-26 00:57

i have following code.

for i in 0..sold.length-1
    duplicate = sold[i]
    print duplicate.check_duplicates
    print \"     \"
    print sold[i].lotnumber + \         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-26 01:29

    The hash resulting from the group_by method can be used as a counting device; this does not use the check_duplicates method.

    grouped = sold.group_by{|item| [item.lotnumber, item.serialnumber]}
    grouped.each{|key, value| puts "#{value.size}   #{key.first}\t#{key.last}"}
    

提交回复
热议问题