Message Boards Message Boards

How can I get to new lists?

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.

POSTED BY: M.A. Ghorbani
23 Replies
Posted 5 years ago

Dear Hans, The solution is the best.

Thank you for your time.

POSTED BY: Alex Teymouri
Posted 5 years ago

That's great, Rohit.

Thank so much.

POSTED BY: Alex Teymouri
Posted 5 years ago

One alternative is to use pattern matching with ReplaceRepeated

list //. {p___, m__, n_, q___} :> {p, m, q} /; Plus[m] == n
POSTED BY: Hans Milton
Posted 5 years ago

Hi Alex,

I assume you are looking for a generic solution that removes elements that equal the sum of preceding elements. Here is one way to do it.

excludeSums[sum_, x_] := If[sum == x, 0, Sow[x]; sum + x];

list = {4, 3, 11, 18, 7, 1, 8, 10, 20, 15, 40, 85};
First@Last@Reap@FoldList[excludeSums, 0, list]

(* {4, 3, 11, 7, 1, 10, 20, 15, 40} *)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Dear Hans,

Say I have a list like this; list={4,3,11,18,7,1,8,10,20,15,40,85 }

We now 4+3+11=18, 7+1=8 and 10+20+15+40=85. How do I delete these elements {18,8,85} from the list?

Thank you for your time.

POSTED BY: Alex Teymouri
Posted 5 years ago

Dear Hans,

Say I have a list like this; list={4,3,11,18,7,1,8,10,20,15,40,85 }

We now 4+3+11=18, 7+1=8 and 10+20+15+40=85. How do I delete these elements {18,8,85} from the list?

Thank you for your time.

POSTED BY: Alex Teymouri
Posted 5 years ago

Thank you for sharing the information.

POSTED BY: LEMUEL OTTO

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 BY: Claudio Chaib
Posted 5 years 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 BY: Hans Milton

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 BY: Claudio Chaib

Very very nice solutions.

Thank so much Claudio ,Hand and Michael.

POSTED BY: M.A. Ghorbani
Posted 5 years 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 BY: Hans Milton
Posted 5 years 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]
POSTED BY: Michael Helmle

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 BY: Claudio Chaib
Posted 5 years 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 BY: Hans Milton

Yes.

That's right. Thank you very much.

POSTED BY: M.A. Ghorbani

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]
POSTED BY: Claudio Chaib

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 BY: M.A. Ghorbani

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, _]}]
POSTED BY: Claudio Chaib

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 BY: Claudio Chaib

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 BY: M.A. Ghorbani

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}]}]
POSTED BY: Claudio Chaib
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 BY: Mariusz Iwaniuk
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