From: dh on 19 May 2010 07:02 Hi, this is easily done using ReplaceAll and pattern matching; testList /. {_, {3, 4}} -> {100, {3, 4}} cheers, Daniel Am 18.05.2010 12:12, schrieb ishii.mitsuo.titanium(a)nsc.co.jp: > testList={ {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} } -- Daniel Huber Metrohm Ltd. Oberdorfstr. 68 CH-9100 Herisau Tel. +41 71 353 8585, Fax +41 71 353 8907 E-Mail:<mailto:dh(a)metrohm.com> Internet:<http://www.metrohm.com>
From: David Park on 19 May 2010 07:04 Use a Rule with ReplaceAll (/.). testList /. {_, {3, 4}} -> {100, {3, 4}} {{11, {2, 3}}, {100, {3, 4}}, {34, {5, 6}}, {51, {7, 8}}} David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: ishii.mitsuo.titanium(a)nsc.co.jp [mailto:ishii.mitsuo.titanium(a)nsc.co.jp] 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: chris on 19 May 2010 07:04 { {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} }/.{x_Integer,{3,4}}->{100,{3,4}} ishii.mitsuo.titanium(a)nsc.co.jp a =E9crit : 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: ishii.mitsuo.titanium on 20 May 2010 06:39 Hi I received many useful suggestions like these below. Sorry for delayed response. Many thanks Mitsuo (1) pos=Position[testList,{_,{3,4}}] newlist=ReplacePart[testList, pos->{100,{3,5}}] (2) testList /. {_, {3, 4}} -> {100, {3, 4}} (3) ClearAll[sub]; sub[expr_, find_, replace_] := Replace[expr, {_, find} -> {replace, find}, {1}]; (4) 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}}} (5) testList /. {_, {3, 4}} -> {100, {3, 4}} {{11, {2, 3}}, {100, {3, 4}}, {34, {5, 6}}, {51, {7, 8}}} (6) { {11,{2,3}},{21,{3,4}},{34,{5,6}},{51,{7,8}} }/.{x_Integer,{3,4}}->{100,{3,4}}
First
|
Prev
|
Pages: 1 2 Prev: Random number generation( b/w two limits) with a Next: combining ArrayPlot with ListLinePlot |