[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: how to remove dups from 2 lists?

Alex Gutteridge

5/11/2007 4:46:00 AM

On 11 May 2007, at 11:18, Mike Steiner wrote:

> def RemoveDupsFromLists ( list1 , list2 )
> list1.each_index do | i |
> list2.each_index do | j |
> if list1[i] == list2[j]
> list1.delete_at ( i )
> list2.delete_at ( j )
> end
> end
> end
> return [ list1 , list2 ]
> end

Here is how I would write it: (it didn't give quite the same results
as your method but seems to be closer to what you asked for so...):

def remove_duplicates(list1,list2)
(list1 & list2).each do |x|
list1.delete(x)
list2.delete(x)
end
return list1,list2
end

irb(main):028:0> RemoveDupsFromLists([1,1,2,3,4],[2,2,3,5,5])
=> [[1, 1, 4], [2, 5, 5]]
irb(main):029:0> remove_duplicates([1,1,2,3,4],[2,2,3,5,5])
=> [[1, 1, 4], [5, 5]]

Be careful with your method since it alters the Arrays as you iterate
through them and will give hard to understand results sometimes. In
the example above '2' is a duplicate but is only deleted from the
second array once because of this problem/feature.

Alex Gutteridge

Bioinformatics Center
Kyoto University



11 Answers

Robert Klemme

5/11/2007 7:07:00 AM

0

On 11.05.2007 06:46, Alex Gutteridge wrote:
> On 11 May 2007, at 11:18, Mike Steiner wrote:
>
>> def RemoveDupsFromLists ( list1 , list2 )
>> list1.each_index do | i |
>> list2.each_index do | j |
>> if list1[i] == list2[j]
>> list1.delete_at ( i )
>> list2.delete_at ( j )
>> end
>> end
>> end
>> return [ list1 , list2 ]
>> end
>
> Here is how I would write it: (it didn't give quite the same results as
> your method but seems to be closer to what you asked for so...):
>
> def remove_duplicates(list1,list2)
> (list1 & list2).each do |x|
> list1.delete(x)
> list2.delete(x)
> end
> return list1,list2
> end
>
> irb(main):028:0> RemoveDupsFromLists([1,1,2,3,4],[2,2,3,5,5])
> => [[1, 1, 4], [2, 5, 5]]
> irb(main):029:0> remove_duplicates([1,1,2,3,4],[2,2,3,5,5])
> => [[1, 1, 4], [5, 5]]
>
> Be careful with your method since it alters the Arrays as you iterate
> through them and will give hard to understand results sometimes. In the
> example above '2' is a duplicate but is only deleted from the second
> array once because of this problem/feature.

Sets also come in handy - especially if those lists are large.

Kind regards

robert

Enrique Comba Riepenhausen

5/11/2007 7:55:00 AM

0

On 11 May 2007, at 09:10, Robert Klemme wrote:
>
> Sets also come in handy - especially if those lists are large.
>
> Kind regards
>
> robert

I would actually need to know what you really want to do :( Let me
explain. In a list you can put different elements in the list with
the same values. Unlike in a hash, where the keys must be unique.

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

Cheers,

Enrique Comba Riepenhausen

Brian Candler

5/11/2007 8:23:00 AM

0

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?

Enrique Comba Riepenhausen

5/11/2007 8:28:00 AM

0


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...


SonOfLilit

5/11/2007 12:49:00 PM

0

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.ruby...

On 5/11/07, Kevin Compton <klcompt@gmail.com> 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 <ecomba@mac.com> 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...
> >
> >
> >
>

Lloyd Linklater

5/11/2007 1:17:00 PM

0

I am new to Ruby but I am wondering why it is that no one is using the
uniq call that gets rid of duplicates in an array. Couldn't you join
the two arrays, then call MyJoinedArray.uniq!, then take the resulting
set and format it as you please? I know that you are doing more than
just that. I was mostly wondering why you would not use the built in
call.

--
Posted via http://www.ruby-....

Lloyd Linklater

5/11/2007 2:52:00 PM

0

Kevin Compton wrote:
> I think a big part of it is that there are variations in what we assume
> the
> questioner wanted.
>
> In my case, I interpreted it as:
> 1) remove items that appear in both lists from both lists
> For instance, removeDups ([a, b, c, d], [b, d, f, g]) => [a, c],
> [f,
> g]
> 2) don't go so far as remove more than the common count of dups
> For instance, removeDups ([a, a, b, b, c, d, d], [b, d, d, d, f, g,
> g])
> => [a, a, b, c], [d, f, g, g]
> 3) keep the lists in original order (probably not required but I'm not
> sure)
>
> I guess, it would be nice to have had the *need* demonstrated via
> example or
> clearly stated..
> but its been fun.

I have to say that I am almost certainly being simplistic here but why
cannot we do something like this:

a = [1, 2, 3, 4]
b = [2, 4, 6, 8]

p a
p b

c = a & b
a = a - c
b = b - c

p c
p a
p b

result:
[1, 2, 3, 4]
[2, 4, 6, 8]
[2, 4]
[1, 3]
[6, 8]

--
Posted via http://www.ruby-....

Harry Kakueki

5/11/2007 3:06:00 PM

0

On 5/11/07, Lloyd Linklater <lloyd@2live4.com> wrote:
> Kevin Compton wrote:
> > I think a big part of it is that there are variations in what we assume
> > the
> > questioner wanted.
> >
> > In my case, I interpreted it as:
> > 1) remove items that appear in both lists from both lists
> > For instance, removeDups ([a, b, c, d], [b, d, f, g]) => [a, c],
> > [f,
> > g]
> > 2) don't go so far as remove more than the common count of dups
> > For instance, removeDups ([a, a, b, b, c, d, d], [b, d, d, d, f, g,
> > g])
> > => [a, a, b, c], [d, f, g, g]
> > 3) keep the lists in original order (probably not required but I'm not
> > sure)
> >
> > I guess, it would be nice to have had the *need* demonstrated via
> > example or
> > clearly stated..
> > but its been fun.
>
> I have to say that I am almost certainly being simplistic here but why
> cannot we do something like this:
>
> a = [1, 2, 3, 4]
> b = [2, 4, 6, 8]
>
> p a
> p b
>
> c = a & b
> a = a - c
> b = b - c
>
> p c
> p a
> p b
>
> result:
> [1, 2, 3, 4]
> [2, 4, 6, 8]
> [2, 4]
> [1, 3]
> [6, 8]
>
> --
> Posted via http://www.ruby-....
>
>
What happens here? :)
All 5's are deleted.
I don't know if that is what he wants but I interpreted it differently.

a = [1, 2, 3, 4, 5, 5, 5]
b = [2, 4, 6, 8, 5]

p a
p b

c = a & b
a = a - c
b = b - c

p c
p a
p b

Harry

--
http://www.kakueki.com/ruby...
A Look into Japanese Ruby List in English

Lloyd Linklater

5/11/2007 3:21:00 PM

0

Harry Kakueki wrote:

> What happens here? :)
> All 5's are deleted.

If you are asking about my example, there were no 5s in it to begin
with. As was observed earlier, I think that better examples would have
resulted in better code. I was mostly interested in finding a rubyish
way to make it happen with great simplicity. If there is something
slightly different needed, perhaps a tweak or two to the arrays before
applying the differences could handle it.

Anyway, this is my first attempt at writing code to solve a question in
here. I am happy that I was able to come up with something that worked
and post it. :)

yay ruby!

--
Posted via http://www.ruby-....

Harry Kakueki

5/11/2007 3:47:00 PM

0

On 5/12/07, Lloyd Linklater <lloyd@2live4.com> wrote:
>
> Anyway, this is my first attempt at writing code to solve a question in
> here. I am happy that I was able to come up with something that worked
> and post it. :)
>
> yay ruby!
>

Yeah, this is very educational.
I learn from reading posts from others and by trying to solve problems
that other people have.

It's like a lot of mini Ruby Quizzes.

Harry