MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Extracting some elements, members of another list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112463] Re: Extracting some elements, members of another list
  • From: Raffy <adraffy at gmail.com>
  • Date: Thu, 16 Sep 2010 06:00:37 -0400 (EDT)
  • References: <i6q0qo$lc5$1@smc.vnet.net>

On Sep 15, 1:40 am, "Carl K. Woll" <ca... at wolfram.com> wrote:
> On 9/14/2010 4:14 AM, Nacho wrote:
>
>
>
>
>
> > Hi!
>
> > I would like your advice about this problem:
>
> > I have a list of elements, consisting in phone numbers with some data:
>
> > list1= { { phone1, data1}, {phone2, data2}, {phone3, data3} .... }
>
> > And a list of phones, a subset of the phone numbers in list1
>
> > list2= {phone2, phone3, phone7... }
>
> > I'd like to extract the elements from list1 whose phone numbers are
> > present in list 2, that is:
>
> > result= { { phone2, data2}, {phone3, data3, {phone7, data7} .... }
>
> > I've used this with small lists and it works fine:
>
> > result = Select[list1, MemberQ[list2, #[[1]]]&];
>
> > The problem is that now I would like to use it with big lists. list1
> > is over 1.000.000 elements long and list2 is about 500.000 elements
> > long.  Ordering is not a problem, I could resort the lists.
>
> > Any hint to extract this list faster? It seems to take a lot of time
> > (estimation is about 5 hours and I have to do it repeatedly)
>
> > Thanks!
>
> One possibility is to use an auxiliary function. I'll call it hashed.
>
> Clear[hashed]
> SetAttributes[hashed, Listable]
> With[{lhs = hashed[list1[[All, 1]]]}, lhs = list1];
>
> Then, use:
>
> hashed[list2]
>
> to get your result.
>
> Carl Woll
> Wolfram Research

More generalized:

ClearAll[find];
find[keys_, vals_, find_, hole_: $Failed] := Module[{x},
   Evaluate[x /@ keys] = vals;
   x[_] = hole;
   x /@ find
   ];

Called via:

find[list1[[All,1]], list1[[All,2]], list2]

Although, if you need to do multiple lookups, you'd want to save the
mapping.


  • Prev by Date: Re: get a table of coordinates from an image of a graph
  • Next by Date: ANOVA question
  • Previous by thread: Re: Extracting some elements, members of another list
  • Next by thread: Re: Extracting some elements, members of another list