Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.3K 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

Thank you a lot. Cheers, 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

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

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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard