Message Boards Message Boards

0
|
8010 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How do I replace some elements of my list? {{a,b},{c,d}} ->{{1,b},{2,d}}

Hi, I want replace some elements of my list "res" .


res = {{{a,b},{c,d},{e,f}}}

lis ={{1,2,3}}

I 'd like obtein :

rep = {{{1,b},{2,d},{3,f}}}

How to do with the command ReplacePart?

Thanks a lot

5 Replies

Hi,

here is a solution with ReplaceAll

ReplaceAll[res, MapThread[Rule, {res[[1, All, 1]], lis[[1]]}]]

and here is one without Replace at all:

Transpose[{lis[[1]], res[[1, All, 2]]}]

Cheers, Marco

PS: Do you really need ReplacePart?

POSTED BY: Marco Thiel

Hi, very good reponse. You are right, I haven't need use "ReplacePart".

I notice that you have practice with Mapthread. So, I could ask you another question. If I use MapThread for Select, where is the position of select condition (I 'd like create a list where the elements are <=2) ? for exemple If I have this:


"res1= {1,2,3}" the command will be "Lat = Select[ res1 , #<=2. &];"


but with res= {{1,2,3},{1,2,3},{1,4,5}} . Which is the command to have:


Lat = {{1,2},{1,2},{1,}}


MapThread[Select, res] (where is the position for the select condition?)

Thank you.

Margherita

Again, I am not sure whether you want MapThread here. You could try Map:

Map[Select[#, # <= 2 &] &, res]

or the short hand notation for that same thing:

Select[#, # <= 2 &] & /@ res

Cheers,

Marco

POSTED BY: Marco Thiel

Thank you a lot. Cheers, Margherita

Hi Marco, I have applied your suggestions in my problems. I have another questions for a similar cas " ReplaceAll[res, MapThread[Rule, {res[[1, All, 1]], lis[[1]]}]]".


I have GG1 = {{0.220067, 0.440134, 0.634358, 0.828582, 1.}, {0.220067, 0.440134, 0.634358, 0.828582, 1.}}


res = {{{0.117779, 0.406482}, {0.235558, 0.333125}, {0.339506, 0.464985}, {0.443454, 0.544808}, {0.535196, 0.61788}}, {{0.117779, 0.406482}, {0.235558, 0.333125}, {0.339506, 0.464985}, {0.443454, 0.544808}, {0.535196, 0.61788}}}

If I do :


ReplaceAll[res, MapThread[Rule, {result[[All, All, 1]], GG1 }]] it doesn't run. Why? result[[All, All, 1]] and GG1 have the same dimensions. The problem occurs because res has too many parenthesis?

Thank you

Cheers, Margherita

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