Message Boards Message Boards

GROUPS:

Coloring the elements of a list with different colors

Posted 4 months ago
756 Views
|
4 Replies
|
3 Total Likes
|

Hi friends,

How do I have the main list with different colors?

Thank you so much for your time.

enter image description here

4 Replies
Posted 4 months 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 4 months ago

Here is another way:

mainList /.
{n_Integer?(CurryApplied[2][MemberQ][listRed])  :> Style[n, Red],
 n_Integer?(CurryApplied[2][MemberQ][listBlue]) :> Style[n, Blue]}
Posted 4 months ago

Thank you so much, Rohit and Gustavo :)

Posted 4 months ago

Hi Gustavo,

+1, nice use of PatternTest and CurryApplied.

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