From: SonOfLilit Date: 2007-05-11T21:48:39+09:00 Subject: Re: how to remove dups from 2 lists? I envision using sets: a = [a, b, c, d, e, e] b = [d, e, f, f, g] dups = a.to_set & b a -= dups # => [a, b, c] b -= dups # => [f, f, g] Or how about just using #-? a = [a, b, c, d, e, e] b = [d, e, f, f, g] c = a.dup d = b.dup b -= c # => [a, b, c] a -= d # => [f, f, g] Do these fit? Aur P.S. have you had a look at http://RubyMentor.rubyforge.org/ On 5/11/07, Kevin Compton wrote: > Assuming my previous assumption of what exactly is needed... at this point > it's academic anyway, right :> > > This will produce the same result with much cleaner code than my previous > post: > > > APPENDAGE_START = "_" > > def make_items_unique(list) > hsh = Hash.new { |h,k| h[k] = 0} > list_mod = list.collect do |x| > hsh[x] += 1 > x.to_s + APPENDAGE_START + hsh[x].to_s > end > end > > list1 = %w{one one two three four four five} > list2 = %w{one three three four five five five} > > puts "before --- list1:#{list1}" > puts "before --- list2:#{list2}" > > list1_mod = make_items_unique(list1) > list2_mod = make_items_unique(list2) > > list3 = list1_mod - list2_mod > list4 = list2_mod - list1_mod > > list1 = list3.collect { |x| x.split(APPENDAGE_START)[0] } > list2 = list4.collect { |x| x.split(APPENDAGE_START)[0] } > > puts "after --- list1:#{list1}" > puts "after --- list2:#{list2}" > > --------------- > output= > before --- list1:oneonetwothreefourfourfive > before --- list2:onethreethreefourfivefivefive > after --- list1:onetwofour > after --- list2:threefivefive > > > On 5/11/07, Enrique Comba Riepenhausen wrote: > > > > > > On 11 May 2007, at 10:23, Brian Candler wrote: > > > > > On Fri, May 11, 2007 at 04:55:14PM +0900, Enrique Comba > > > Riepenhausen wrote: > > >> Do you want to remove the elements that are in the same position on > > >> the lists and are equal? > > >> > > >> If so I would say: > > >> > > >> require 'generator' > > >> > > >> list1 = [1,1,2,3,4,6] # => I included the 6 to show what I mean... > > >> list2 = [2,2,3,5,5,6] # => I included the 6 to show what I mean... > > >> > > >> def RemoveDupsFromLists ( list1 , list2 ) > > >> lists = SyncEnumerator.new(list1, list2) > > >> lists.each { |element_list1, element_list2| > > >> > > >> if list1[element_list1] == list2[element_list2] > > >> list1.delete(element_list1) > > >> list2.delete(element_list2) > > >> end > > >> } > > >> end > > > > > > Is it safe to delete from lists while you're enumerating through them? > > > > Actually not ;) It depends if other objects are trying to access > > those lists at the same time though... > > > > > > >