Message Boards Message Boards

GROUPS:

How can I get to new lists?

Posted 1 month ago
485 Views
|
17 Replies
|
19 Total Likes
|

Dear All,

How do I able to reach to new lists as follow:

enter image description here

m = {2, "*", 4, "*"}

n = {8, "*", 7, 9}

mNEW = {2, 4}

nNEW = {8, 7}

Thank you very much.

17 Replies
m = {2, "*", 4, "*"}; n = {8, "*", 7, 9};
mNEW=Take[DeleteCases[m, "*"], 2]
nNEW =Take[DeleteCases[n, "*"], 2]

Or:

m = {2, "*", 4, "*"}; n = {8, "*", 7, 9};
mNEW = DeleteCases[m, "*"][[1 ;; 2]]
nNEW = DeleteCases[n, "*"][[1 ;; 2]]

Regards M.I.

Posted 1 month ago

This is another way not as automatic as the DeleteCases command but by manually placing each position.

m = {2, "*", 4, "*"};
n = {8, "*", 7, 9};
mNew = Flatten[{Take[m, {1}], Take[m, {3}]}]
nNew = Flatten[{Take[n, {1}], Take[n, {3}]}]

Dear Mariusz and Claudio,

Your methods can be applied for an individual list.

I want to choose elements from list m, according to exist elements in list n.

Thank you for your consideration.

Posted 1 month ago

Do you want to relate the elements by their value or by the positions in the table?

Here is for the positions:

m = {2, "*", 4, "*", 6, 
   "*", 8, "*", 12, 
   "*"};
n = {8, "*", 7, "*", 11, 
   "*", 5, "*", 9, 
   "*"};
mNew = Flatten[Table[Take[m, {2*i - 1}], {i, 1, (Count[m, _] + 1)/2}]]
nNew = Flatten[Table[Take[n, {2*j - 1}], {j, 1, (Count[n, _] + 1)/2}]]

I don't know if it helps.

Posted 1 month ago

If you want to limit the quantity and by the positions of the elements by the list "nNew " can be done in this way:

m = {2, "*", 4, "*", 6, 
   "*", 8, "*", 12, 
   "*", 14};
n = {8, "*", 7, "*", 11};
mNew = Take[
  Flatten[Table[Take[m, {2*i - 1}], {i, 1, (Count[m, _] + 1)/2}]], {1,
    Count[nNew, _]}]
nNew = Flatten[Table[Take[n, {2*j - 1}], {j, 1, (Count[n, _] + 1)/2}]]

That way when m >n, if n > m can be like this:

m = {2, "*", 4, "*", 6, 
   "*", 8, "*", 12, 
   "*", 14};
n = {8, "*", 7, "*", 11, 
   "*", 13, "*", 15, 
   "*", 17, "*", 21};
mNew = Flatten[Table[Take[m, {2*i - 1}], {i, 1, (Count[m, _] + 1)/2}]]
nNew = Take[
  Flatten[Table[Take[n, {2*j - 1}], {j, 1, (Count[n, _] + 1)/2}]], {1,
    Count[mNew, _]}]

Claudio,

I evaluated your last solution way for the below lists. The correct answer should be :

mNEW={2,4,17}

nNew={8,7,14}

enter image description here

In[23]:= m = {2, "*", 4, "*", 10, 17}

n = {8, "*", 7, 9, "*", 14}

In[25]:= mNew = 
 Flatten[Table[Take[m, {2*i - 1}], {i, 1, (Count[m, _] + 1)/2}]]
nNew = Take[
  Flatten[Table[Take[n, {2*j - 1}], {j, 1, (Count[n, _] + 1)/2}]], {1,
    Count[mNew, _]}]

Out[25]= {2, 4, 10}

Out[26]= {8, 7, "*"}
Posted 1 month ago

Now I think I understand what you want to do, only when it's numbers in the two sets they appear in the answer. Check if this was helpful...

m = {2, "*", 4, "*", 10, 17, 19, 22};
n = {8, "*", 7, 9, "*", 14};
mNew = Select[
  Flatten[Table[
    If[FromDigits[Take[m, {i}]] + FromDigits[Take[n, {i}]] \[Element] 
      Reals, Take[m, {i}]], {i, 1, 
     If[Count[n, _] > Count[m, _], Count[m,_], Count[n, _]]}]], 
  NumberQ]
nNew = Select[
  Flatten[Table[
    If[FromDigits[Take[m, {j}]] + FromDigits[Take[n, {j}]] \[Element] 
      Reals, Take[n, {j}]], {j, 1, 
     If[Count[n, _] > Count[m, _], Count[m,_], Count[n, _]]}]], 
  NumberQ]

Yes.

That's right. Thank you very much.

Posted 1 month ago
m = {2, "*", 4, "*", 10, 17};
n = {8, "*", 7, 9, "*", 14};

In[3]:= Transpose@Select[Transpose@{m, n}, VectorQ[#, NumberQ] &]
Out[3]= {{2, 4, 17}, {8, 7, 14}}
Posted 1 month ago

That's nice, Hans, but is there a way we can use this method if by chance the lists don't have the same number of elements? Is this possible? Thank you.

Posted 1 month ago

If the lists do not have the same length it will be necessary to pad the shortest list, to get equal lengths.

p = {2, "*", 4, "*", 10, 17, 19, 22};
q = {8, "*", 7, 9, "*", 14};

In[15]:= Transpose@Select[Transpose@{p, PadRight[q, 8, x]}, AllTrue[#, NumberQ] &]
Out[15]= {{2, 4, 17}, {8, 7, 14}}
Posted 1 month ago

Thank you Hans. I know I'll have to change something in the code to make this other example... In this case what would it be?

p = {2, "*", 4, "*", 10};
q = {8, "*", 7, 9, "*", 14, 7, "*", 11};

Because without changing anything in the code it generated:

enter image description here

Thanks again for your reply!

Posted 1 month ago

Claudio, you padded the longest list, not the shortest. Therefore the error.

This version is more tolerant:

In[1]:= m = {2, "*", 4, "*", 10, 17, 19, 22}
        n = {8, "*", 7, 9, "*", 14

In[3]:= Transpose@Select[Transpose@PadRight[{m, n}, {2, 8}, "*"], AllTrue[#, NumberQ] &]    
Out[3]= {{2, 4, 17}, {8, 7, 14}}

In[4]:= p = {8, "*", 7, 9, "*", 14}
        q = {2, "*", 4, "*", 10, 17, 19, 22}

In[6]:= Transpose@Select[Transpose@PadRight[{p, q}, {2, 8}, "*"], AllTrue[#, NumberQ] &]   
Out[6]= {{8, 7, 14}, {2, 4, 17}}
Posted 1 month ago

Thank you very much Hans! ... Now I could understand the code and could make a simple modification to multiple lists... Thanks to you I managed to learn this new tool, thank you!

m = {3, "*", 5, 11, "*", 21, 14, 13, "*", "*", 5};
n = {8, "*", "*", 9, "*", 14, 13, 18, "*", "*", 1, 15};
o = {2, "*", 4, "*", 10, 17, 19, "*"};

b = FromDigits[
   Take[Sort[{Count[m, _], Count[n, _], Count[o, _]}], {1}]];
z = Transpose@
   Select[Transpose@PadRight[{m, n, o}, {3, b}, "*"], 
    AllTrue[#, NumberQ] &];
mNew = Flatten[Take[z, {1}]]
nNew = Flatten[Take[z, {2}]]
oNew = Flatten[Take[z, {3}]]

enter image description here

Posted 1 month ago

How about this?

m = {2, "*", 4, "*", 10, 17};
n = {8, "*", 7, 9, "*", 14};
lst1 = {m, n}\[Transpose];

Now lst1 is processed to remove the "*":

lst2 = DeleteCases[lst1, "*", 2]

Finally all sublists which are shorter than2 elements are being removed and the result is transposed:

DeleteCases[lst2, _?(Length[#] < 2 &)]\[Transpose]

Very very nice solutions.

Thank so much Claudio ,Hand and Michael.

Posted 1 month ago

Thank you for sharing the information.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract