Re: Extracting some elements, members of another list
- To: mathgroup at smc.vnet.net
- Subject: [mg112444] Re: Extracting some elements, members of another list
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Wed, 15 Sep 2010 20:03:08 -0400 (EDT)
- References: <201009140914.FAA16675@smc.vnet.net>
Hi, This is a follow-up to my previous post. Since you mentioned that the order of elements is not an issue, there is another general solution, both simpler and faster than the one based on memberPositions function, but slower than that based on memberPositionsInteger function from my previous post: The lists: In[1]:= list1 = Transpose[{RandomSample[Range[1000000,9999999],1000000],Table[ToExpression["data"<>ToString[i]],{i,1,1000000}]}]; In[2]:= list2 = RandomSample[list1[[All,1]],500000]; The solution: In[3]:= list2/.Dispatch[Thread[#[[All,1]]->#]&[list1]]//Short//Timing Out[3]= {3.234,{{6059339,data697996},{6645675,data173455},<<499996>>,{5345183,data604454},{8230717,data405701}}} The price to pay is that here the original ordering of the phone data is lost. The other difference w.r.t the solution based on memberPositions is that if some phones are repeated with different data attached, this one will repeat the first entry rather than list all different entries correctly, which is what the memberPositions-based solution does. The more specialized solution from my previous reply is still about 1.5x faster than this one and does preserve the original ordering. Regards, Leonid On Tue, Sep 14, 2010 at 1:14 PM, Nacho <ncc1701zzz at gmail.com> 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! > >
- References:
- Extracting some elements, members of another list
- From: Nacho <ncc1701zzz@gmail.com>
- Extracting some elements, members of another list