# How can I get to new lists?

Posted 2 years ago
6032 Views
|
23 Replies
|
24 Total Likes
|
 Dear All,How do I able to reach to new lists as follow: m = {2, "*", 4, "*"} n = {8, "*", 7, 9} mNEW = {2, 4} nNEW = {8, 7} Thank you very much.
23 Replies
Sort By:
Posted 2 years ago
 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 2 years 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}]}] 
Posted 2 years ago
 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 2 years 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 2 years 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, _]}] 
Posted 2 years ago
 Claudio,I evaluated your last solution way for the below lists. The correct answer should be :mNEW={2,4,17}nNew={8,7,14} 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 2 years 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] 
Posted 2 years ago
 Yes. That's right. Thank you very much.
Posted 2 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 2 years 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 2 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 2 years 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:Thanks again for your reply!
Posted 2 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 2 years 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}]] 
Posted 1 year 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 1 year 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 1 year 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 1 year ago
 That's great, Rohit. Thank so much.
Posted 1 year ago
 One alternative is to use pattern matching with ReplaceRepeated list //. {p___, m__, n_, q___} :> {p, m, q} /; Plus[m] == n 
Posted 1 year ago
 Dear Hans, The solution is the best.Thank you for your time.
Posted 2 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]