Message Boards Message Boards

0
|
2761 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Coloring the elements of a list with different colors

Posted 3 years ago

Hi friends,

How do I have the main list with different colors?

Thank you so much for your time.

enter image description here

POSTED BY: Alex Teymouri
4 Replies

Here is another way:

mainList /.
{n_Integer?(CurryApplied[2][MemberQ][listRed])  :> Style[n, Red],
 n_Integer?(CurryApplied[2][MemberQ][listBlue]) :> Style[n, Blue]}
POSTED BY: Gustavo Delfino
Posted 3 years ago

Thank you so much, Rohit and Gustavo :)

POSTED BY: Alex Teymouri
Posted 3 years ago

Hi Gustavo,

+1, nice use of PatternTest and CurryApplied.

POSTED BY: Rohit Namjoshi
Posted 3 years ago

Alex,

Please post code, not images.

mainList = {12, 14, 8, 0, 33, 17, 5, 10};
listRed = {12, 8, 0, 10};

If[MemberQ[listRed, #], Style[#, Red], #] & /@ mainList

listBlue = Complement[mainList, listRed];
colorMap = AssociationMap[Red &, listRed]~Join~AssociationMap[Blue &, listBlue]

Style[#, colorMap[#]] & /@ mainList
POSTED BY: Rohit Namjoshi
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