Re: Element Extraction
- To: mathgroup at smc.vnet.net
- Subject: [mg19272] Re: [mg19240] Element Extraction
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 12 Aug 1999 01:24:25 -0400
- Sender: owner-wri-mathgroup at wolfram.com
>Hello everyone, >I have a problem extracting list of list. >Given some list, consisting of elements (not all of them distinct) with >the same length and some target list of the same length, for example: >In[26]:= >someList={{0,0,0},{0,0,1},{0,0,2},{0,1,0},{0,1,1},{0,1,2},{0,2,0},{0,2,1},{0, >2,2},{1,0,0},{1,0,1},{1,0,2},{1,1,0},{1,1,1},{1,1,2},{1,2,0},{1,2,1},{1, >2,2},{2,0,0},{2,0,1},{2,0,2},{2,1,0},{2,1,1},{2,1,2},{2,2,0},{2,2,1},{2, > 2,2}}; > >In[27]:= >target={0,0,1}; > >Hamming distance between two bit strings means the number of bit >positions in which they differ. For example consecutive elements of the >Gray code list have Hamming distance = 1. >I need to extract these cases(elements of someList) which differ from >target in one coordinate (Hamming distance = 1) or less (the >element===target itself//Hamming distance=0), so the answer should be: > >In[33]:= >answer={{0,0,0},{0,0,1},{0,0,2},{0,1,1},{0,2,1},{1,0,1},{2,0,1}}; > >How could I achieve the extraction efficiently? >Thank you in advance for any suggestions. >Eugene > Eugene, This is one approach: hammtest[p : {_, _, _}, t : {_, _, _}] := Count[Abs[t - p], 0] > 1; Select[someList, hammtest[#, {0, 0, 1}] &] {{0, 0, 0}, {0, 0, 1}, {0, 0, 2}, {0, 1, 1}, {0, 2, 1}, {1, 0, 1}, {2, 0, 1}} On this test list: testlist = Table[{Random[Integer, {0, 2}], Random[Integer, {0, 2}], Random[Integer, {0, 2}]}, {10000}]; and on a Pentium 166 MH PC, the timing was Select[testlist, hammtest[#, {0, 0, 1}] &]; // Timing {5.82 Second, Null} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/