From: ishii.mitsuo.titanium on 18 May 2010 06:12 Hi I have a problem. I have a list. testList={ {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} } 1) From this list, select any element list such as {x,{3,4}}, where X is an arbitrary number. In general, position of {x,{3,4}} in "testList" is not fixed. 2) Select any element list such as {x,{3,4}} and then exchange {x,{3,4}} to {100,{3,4}}. 3)Make a new list finally. testListNew={ {11,{2,3}},{100,{3,4}},{34,{5,6}},{51,{7,8}} } Please teach me how to solve. Thanks in advance. Mitsuo Ishii
From: Bob Hanlon on 19 May 2010 07:00 testList = {{11, {2, 3}}, {21, {3, 4}}, {34, {5, 6}}, {51, {7, 8}}, {77, {3, 4}}}; Cases[testList, {_, {3, 4}}] {{21, {3, 4}}, {77, {3, 4}}} or Select[testList, #[[2]] == {3, 4} &] {{21, {3, 4}}, {77, {3, 4}}} Flatten[Position[testList, {_, {3, 4}}]] {2,5} However, none of this is needed. Just use testList /. {_, {3, 4}} -> {100, {3, 4}} {{11, {2, 3}}, {100, {3, 4}}, {34, {5, 6}}, {51, {7, 8}}, {100, {3, 4}}} Bob Hanlon ---- ishii.mitsuo.titanium(a)nsc.co.jp wrote: ============= Hi I have a problem. I have a list. testList={ {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} } 1) From this list, select any element list such as {x,{3,4}}, where X is an arbitrary number. In general, position of {x,{3,4}} in "testList" is not fixed. 2) Select any element list such as {x,{3,4}} and then exchange {x,{3,4}} to {100,{3,4}}. 3)Make a new list finally. testListNew={ {11,{2,3}},{100,{3,4}},{34,{5,6}},{51,{7,8}} } Please teach me how to solve. Thanks in advance. Mitsuo Ishii
From: Raffy on 19 May 2010 07:01 On May 18, 3:12 am, ishii.mitsuo.titan...(a)nsc.co.jp wrote: > Hi > > I have a problem. > > I have a list. > > testList={ {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} } > > 1) From this list, select any element list such as {x,{3,4}}, > > where X is an arbitrary number. > > In general, position of {x,{3,4}} in "testList" is not fixed. > > 2) Select any element list such as {x,{3,4}} and then exchange {x,{3,4}} to {100,{3,4}}. > > 3)Make a new list finally. > > testListNew={ {11,{2,3}},{100,{3,4}},{34,{5,6}},{51,{7,8}} } > > Please teach me how to solve. > > Thanks in advance. > > Mitsuo Ishii ClearAll[sub]; sub[expr_, find_, replace_] := Replace[expr, {_, find} -> {replace, find}, {1}]; sub[{{11, {2, 3}}, {21, {3, 4}}, {34, {5, 6}}, {51, {7, 8}}}, {3, 4}, 100] ==> {{11, {2, 3}}, {100, {3, 4}}, {34, {5, 6}}, {51, {7, 8}}}
From: Bill Rowe on 19 May 2010 07:02 On 5/18/10 at 6:12 AM, ishii.mitsuo.titanium(a)nsc.co.jp wrote: >I have a list. >testList={ {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} } >1) From this list, select any element list such as {x,{3,4}}, >where X is an arbitrary number. either In[5]:= Cases[testList, {_, {3, 4}}] Out[5]= {{21, {3, 4}}} or In[6]:= Select[testList, Last@# === {3, 4} &] Out[6]= {{21, {3, 4}}} will do this >In general, position of {x,{3,4}} in "testList" is not fixed. >2) Select any element list such as {x,{3,4}} and then exchange >{x,{3,4}} to {100,{3,4}}. >3)Make a new list finally. >testListNew={ {11,{2,3}},{100,{3,4}},{34,{5,6}},{51,{7,8}} } This last can be accomplished in one step as: In[7]:= testList /. {_, {3, 4}} -> {100, {3, 4}} Out[7]= {{11, {2, 3}}, {100, {3, 4}}, {34, {5, 6}}, {51, {7, 8}}}
From: Peter Breitfeld on 19 May 2010 07:02 ishii.mitsuo.titanium(a)nsc.co.jp wrote: > Hi > > I have a problem. > > > I have a list. > > testList={ {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} } > > 1) From this list, select any element list such as {x,{3,4}}, > > where X is an arbitrary number. > > In general, position of {x,{3,4}} in "testList" is not fixed. > > 2) Select any element list such as {x,{3,4}} and then exchange {x,{3,4}} to {100,{3,4}}. > > 3)Make a new list finally. > > testListNew={ {11,{2,3}},{100,{3,4}},{34,{5,6}},{51,{7,8}} } > > Please teach me how to solve. > > Thanks in advance. > > Mitsuo Ishii > First search, where the member is pos=Position[testList,{_,{3,4}}] newlist=ReplacePart[testList, pos->{100,{3,5}}] -- _________________________________________________________________ Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
|
Next
|
Last
Pages: 1 2 Prev: Random number generation( b/w two limits) with a Next: combining ArrayPlot with ListLinePlot |