0
$\begingroup$
assoc = Association[F67 -> {S51, S90}, F1 -> {S1, S43}, 
   F2 -> {S32, S51, S62, S65}, F3 -> {}, F4 -> {S8, S32, S51}, 
   F5 -> {S1, S43, S44}, F6 -> {S51, S55, S56}, 
   F7 -> {S45, S51, S55, S56}, F8 -> {S51, S55, S56}, 
   F9 -> {S1, S3, S6, S10, S32, S43, S55, S58, S73, S86}, 
   F11 -> {S51, S76}, F12 -> {S51, S76}, 
   F13 -> {S28, S51, S56, S71, S76, S95}, F14 -> {}, 
   F15 -> {S51, S98}, F16 -> {S39, S42, S43, S90}, F17 -> {}, 
   F18 -> {S50, S51, S67, S87}, F19 -> {S51}, F20 -> {S51}, 
   F21 -> {S44, S51, S74}, F22 -> {S1, S37, S43, S46, S100}, 
   F23 -> {S7, S10, S20, S21, S22, S23, S24, S25, S26, S30, S33, S51, 
     S52, S53, S63, S64, S68, S90, S91, S99}, 
   F24 -> {S51, S63, S88, S90}, 
   F25 -> {S51, S52, S62, S83, S84, S85, S88, S90, S97}, F66 -> {}, 
   F26 -> {S10, S27, S41, S51, S52, S57, S59, S62, S79, S83, S84, S85,
      S88, S90, S93, S94, S97}, 
   F27 -> {S5, S9, S10, S19, S27, S41, S44, S47, S51, S52, S57, S59, 
     S62, S79, S83, S84, S85, S88, S90, S93, S94, S97}, 
   F28 -> {S15, S18, S48, S49, S51, S54, S60, S62, S75, S83, S84, S90,
      S92, S97}, 
   F29 -> {S3, S6, S10, S14, S30, S34, S45, S47, S51, S52, S59, S62, 
     S63, S90}, F30 -> {S45, S51}, F31 -> {S40, S51, S61, S77, S78}, 
   F32 -> {S51}, F33 -> {S51, S57}, 
   F34 -> {S10, S11, S27, S47, S51, S52, S62, S63, S73, S81, S90, 
     S97}, F35 -> {S51}, F36 -> {S50, S51, S67}, 
   F37 -> {S1, S31, S37, S82}, F38 -> {S44, S51}, F39 -> {S51}, 
   F40 -> {S1, S37, S43}, F41 -> {S51}, F42 -> {S51}, 
   F43 -> {S16, S17, S51}, F44 -> {S16, S17, S51}, F45 -> {S44, S51}, 
   F46 -> {S51}, F47 -> {S44, S51}, F48 -> {S1, S37, S71, S100}, 
   F49 -> {S3, S6, S35, S38, S51, S66, S96}, 
   F50 -> {S3, S6, S13, S16, S17, S35, S38, S51, S66, S89, S96}, 
   F51 -> {S13, S16, S17, S27, S35, S38, S51, S80, S89, S96}, 
   F52 -> {S13, S27, S35, S38, S51, S80, S96}, 
   F53 -> {S4, S27, S35, S38, S44, S51, S80, S89, S96}, 
   F54 -> {S38, S51, S72, S80, S89, S96}, 
   F55 -> {S1, S12, S13, S36, S37}, F56 -> {S29, S35, S51, S66}, 
   F57 -> {S29, S35, S51, S66}, F58 -> {S29, S35, S44, S51, S66}, 
   F59 -> {S1, S37}, F60 -> {S2, S51, S70}, F61 -> {S2, S51, S70}, 
   F62 -> {S2, S51, S69, S70}, F63 -> {S43}, F64 -> {S51, S90}, 
   F65 -> {S51, S90}, F66 -> {}, F10 -> {S51, S56, S76}];
KeyTake[%, {F67, F63, F19, F10, F46}]

The output is <|F67 -> {S51, S90}, F63 -> {S43}, F19 -> {S51}, F10 -> {S51, S56, S76}, F46 -> {S51}|> Given this, can one extract a listing or table that orders all Fxx-> by the number of elements contained in the braces of each Fxx and shows the corresponding count of those elements? For example, F67 is associated with 2 elements {S51,S90}, F1 is associated with 2 elements {S1,S43}. I'd like to find all Fxx's that are associated with 0, 1, 2, 3, elements and have that finding ordered by the number of elements in the associated braces.

$\endgroup$
  • $\begingroup$ does Sort@Map[Length]@assoc give what you need? $\endgroup$ – kglr Jan 11 at 14:15
  • $\begingroup$ That helps too but I would like to include the names of each elements in the list that Length operates on $\endgroup$ – PRG Jan 11 at 14:17
  • 1
    $\begingroup$ PRG, you need to use @kglr to ensure that the user is notified of your response. Perhaps SortBy[] would do what you want? $\endgroup$ – Michael E2 Jan 11 at 14:21
  • $\begingroup$ @kglr thank you $\endgroup$ – PRG Jan 11 at 14:27
  • $\begingroup$ @Michael E2 I using your idea I found that just Sort@assoc works too $\endgroup$ – PRG Jan 11 at 14:28
1
$\begingroup$

You may use Query and Select.

Query[Select[Between[{0, 3}]@Length@# &] /* SortBy[Length]]@assoc
<|F14->{},F17->{},F3->{},F66->{},F19->{S51},F20->{S51},F32->{S51},F35->{S51},F39->{S51},F41->{S51},F42->{S51},F46->{S51},F63->{S43},F1->{S1,S43},F11->{S51,S76},F12->{S51,S76},F15->{S51,S98},F30->{S45,S51},F33->{S51,S57},F38->{S44,S51},F45->{S44,S51},F47->{S44,S51},F59->{S1,S37},F64->{S51,S90},F65->{S51,S90},F67->{S51,S90},F10->{S51,S56,S76},F21->{S44,S51,S74},F36->{S50,S51,S67},F4->{S8,S32,S51},F40->{S1,S37,S43},F43->{S16,S17,S51},F44->{S16,S17,S51},F5->{S1,S43,S44},F6->{S51,S55,S56},F60->{S2,S51,S70},F61->{S2,S51,S70},F8->{S51,S55,S56}|>

Adding sorting of the Keys requires a bit of extra work because they are alphanumeric symbols. Converting to strings gets the sort closer but has the issue that "3" > "11". Converting the number strings to integers will get the sort correct. Also a little ascending/descending operator trickery is needed.

Query[Select[Between[{0, 3}]@Length@# &] /* GroupBy[Length] /* 
   KeySort /* (Values@# &) /* Merge[Flatten], 
  KeySortBy[FromDigits@*Curry[StringDrop][1]@*SymbolName]]@assoc
<|F3->{},F14->{},F17->{},F66->{},F19->{S51},F20->{S51},F32->{S51},F35->{S51},F39->{S51},F41->{S51},F42->{S51},F46->{S51},F63->{S43},F1->{S1,S43},F11->{S51,S76},F12->{S51,S76},F15->{S51,S98},F30->{S45,S51},F33->{S51,S57},F38->{S44,S51},F45->{S44,S51},F47->{S44,S51},F59->{S1,S37},F64->{S51,S90},F65->{S51,S90},F67->{S51,S90},F4->{S8,S32,S51},F5->{S1,S43,S44},F6->{S51,S55,S56},F8->{S51,S55,S56},F10->{S51,S56,S76},F21->{S44,S51,S74},F36->{S50,S51,S67},F40->{S1,S37,S43},F43->{S16,S17,S51},F44->{S16,S17,S51},F60->{S2,S51,S70},F61->{S2,S51,S70}|>

Hope this helps.

$\endgroup$
  • $\begingroup$ many thanks, yes that is very nice ... can I use Ordering in some way to have the Fxx ordered like F3->{},F14->{},F17->{}, etc? $\endgroup$ – PRG Jan 11 at 15:32
  • $\begingroup$ @PRG Are your Keys strings or symbols. You have them here as symbols but I think that may be the result of your paste. Similarly, are your Values list of strings or symbols. If they are not symbols please update the question with the correct types. $\endgroup$ – Edmund Jan 11 at 21:58
  • $\begingroup$ My keys are indeed symbols, e.g., F8, F36, etc are names of vertices in a graph; the same with values. $\endgroup$ – PRG Jan 11 at 22:08
  • $\begingroup$ @PRG See update. $\endgroup$ – Edmund Jan 11 at 22:15
  • $\begingroup$ the Curry command doesnt work error is: "The expression Curry[StringDrop][1][F67] is not a list of digits or a string of valid digits" $\endgroup$ – PRG Jan 11 at 22:42
1
$\begingroup$
Map[{Length @ #, ## & @@ #} &] @ SortBy[assoc, Length]

<|F14 -> {0}, F17 -> {0}, F3 -> {0}, F66 -> {0},
F19 -> {1, S51}, F20 -> {1, S51}, F32 -> {1, S51}, F35 -> {1, S51}, F39 -> {1, S51}, F41 -> {1, S51}, F42 -> {1, S51}, F46 -> {1, S51}, F63 -> {1, S43},
F1 -> {2, S1, S43}, F11 -> {2, S51, S76}, F12 -> {2, S51, S76}, F15 -> {2, S51, S98}, F30 -> {2, S45, S51}, F33 -> {2, S51, S57}, F38 -> {2, S44, S51}, F45 -> {2, S44, S51}, F47 -> {2, S44, S51}, F59 -> {2, S1, S37}, F64 -> {2, S51, S90}, F65 -> {2, S51, S90}, F67 -> {2, S51, S90},
F10 -> {3, S51, S56, S76}, F21 -> {3, S44, S51, S74}, F36 -> {3, S50, S51, S67}, F4 -> {3, S8, S32, S51}, F40 -> {3, S1, S37, S43}, F43 -> {3, S16, S17, S51}, F44 -> {3, S16, S17, S51}, F5 -> {3, S1, S43, S44}, F6 -> {3, S51, S55, S56}, F60 -> {3, S2, S51, S70}, F61 -> {3, S2, S51, S70}, F8 -> {3, S51, S55, S56},
F16 -> {4, S39, S42, S43, S90}, F18 -> {4, S50, S51, S67, S87}, F2 -> {4, S32, S51, S62, S65}, F24 -> {4, S51, S63, S88, S90}, F37 -> {4, S1, S31, S37, S82}, F48 -> {4, S1, S37, S71, S100}, F56 -> {4, S29, S35, S51, S66}, F57 -> {4, S29, S35, S51, S66}, F62 -> {4, S2, S51, S69, S70}, F7 -> {4, S45, S51, S55, S56},
F22 -> {5, S1, S37, S43, S46, S100}, F31 -> {5, S40, S51, S61, S77, S78}, F55 -> {5, S1, S12, S13, S36, S37}, F58 -> {5, S29, S35, S44, S51, S66},
F13 -> {6, S28, S51, S56, S71, S76, S95}, F54 -> {6, S38, S51, S72, S80, S89, S96},
F49 -> {7, S3, S6, S35, S38, S51, S66, S96}, F52 -> {7, S13, S27, S35, S38, S51, S80, S96},
F25 -> {9, S51, S52, S62, S83, S84, S85, S88, S90, S97}, F53 -> {9, S4, S27, S35, S38, S44, S51, S80, S89, S96},
F51 -> {10, S13, S16, S17, S27, S35, S38, S51, S80, S89, S96}, F9 -> {10, S1, S3, S6, S10, S32, S43, S55, S58, S73, S86},
F50 -> {11, S3, S6, S13, S16, S17, S35, S38, S51, S66, S89, S96},
F34 -> {12, S10, S11, S27, S47, S51, S52, S62, S63, S73, S81, S90, S97},
F28 -> {14, S15, S18, S48, S49, S51, S54, S60, S62, S75, S83, S84, S90, S92, S97}, F29 -> {14, S3, S6, S10, S14, S30, S34, S45, S47, S51, S52, S59, S62, S63, S90},
F26 -> {17, S10, S27, S41, S51, S52, S57, S59, S62, S79, S83, S84, S85, S88, S90, S93, S94, S97},
F23 -> {20, S7, S10, S20, S21, S22, S23, S24, S25, S26, S30, S33, S51, S52, S53, S63, S64, S68, S90, S91, S99},
F27 -> {22, S5, S9, S10, S19, S27, S41, S44, S47, S51, S52, S57, S59, S62, S79, S83, S84, S85, S88, S90, S93, S94, S97}|>

$\endgroup$
  • $\begingroup$ very nice and compact!! ... thank you! $\endgroup$ – PRG Jan 11 at 23:37
  • $\begingroup$ @PRG, my pleasure. $\endgroup$ – kglr Jan 11 at 23:59
0
$\begingroup$
x = Sort@Map[Length]@assoc
y = Sort@assoc
Merge[{x, y}, Flatten]

This works well and produces

<|F3 -> {0}, F14 -> {0}, F17 -> {0}, F66 -> {0}, F19 -> {1, S51}, 
 F20 -> {1, S51}, F32 -> {1, S51}, F35 -> {1, S51}, F39 -> {1, S51}, 
 F41 -> {1, S51}, F42 -> {1, S51}, F46 -> {1, S51}, F63 -> {1, S43}, 
 F67 -> {2, S51, S90}, F1 -> {2, S1, S43}, F11 -> {2, S51, S76}, 
 F12 -> {2, S51, S76}, F15 -> {2, S51, S98}, F30 -> {2, S45, S51}, 
 F33 -> {2, S51, S57}, F38 -> {2, S44, S51}, F45 -> {2, S44, S51}, 
 F47 -> {2, S44, S51}, F59 -> {2, S1, S37}, F64 -> {2, S51, S90}, 
 F65 -> {2, S51, S90}, F4 -> {3, S8, S32, S51}, 
 F5 -> {3, S1, S43, S44}, F6 -> {3, S51, S55, S56}, 
 F8 -> {3, S51, S55, S56}, F21 -> {3, S44, S51, S74}, 
 F36 -> {3, S50, S51, S67}, F40 -> {3, S1, S37, S43}, 
 F43 -> {3, S16, S17, S51}, F44 -> {3, S16, S17, S51}, 
 F60 -> {3, S2, S51, S70}, F61 -> {3, S2, S51, S70}, 
 F10 -> {3, S51, S56, S76}, F2 -> {4, S32, S51, S62, S65}, 
 F7 -> {4, S45, S51, S55, S56}, F16 -> {4, S39, S42, S43, S90}, 
 F18 -> {4, S50, S51, S67, S87}, F24 -> {4, S51, S63, S88, S90}, 
 F37 -> {4, S1, S31, S37, S82}, F48 -> {4, S1, S37, S71, S100}, 
 F56 -> {4, S29, S35, S51, S66}, F57 -> {4, S29, S35, S51, S66}, 
 F62 -> {4, S2, S51, S69, S70}, F22 -> {5, S1, S37, S43, S46, S100}, 
 F31 -> {5, S40, S51, S61, S77, S78}, 
 F55 -> {5, S1, S12, S13, S36, S37}, 
 F58 -> {5, S29, S35, S44, S51, S66}, 
 F13 -> {6, S28, S51, S56, S71, S76, S95}, 
 F54 -> {6, S38, S51, S72, S80, S89, S96}, 
 F49 -> {7, S3, S6, S35, S38, S51, S66, S96}, 
 F52 -> {7, S13, S27, S35, S38, S51, S80, S96}, 
 F25 -> {9, S51, S52, S62, S83, S84, S85, S88, S90, S97}, 
 F53 -> {9, S4, S27, S35, S38, S44, S51, S80, S89, S96}, 
 F9 -> {10, S1, S3, S6, S10, S32, S43, S55, S58, S73, S86}, 
 F51 -> {10, S13, S16, S17, S27, S35, S38, S51, S80, S89, S96}, 
 F50 -> {11, S3, S6, S13, S16, S17, S35, S38, S51, S66, S89, S96}, 
 F34 -> {12, S10, S11, S27, S47, S51, S52, S62, S63, S73, S81, S90, 
   S97}, F28 -> {14, S15, S18, S48, S49, S51, S54, S60, S62, S75, S83,
    S84, S90, S92, S97}, 
 F29 -> {14, S3, S6, S10, S14, S30, S34, S45, S47, S51, S52, S59, S62,
    S63, S90}, 
 F26 -> {17, S10, S27, S41, S51, S52, S57, S59, S62, S79, S83, S84, 
   S85, S88, S90, S93, S94, S97}, 
 F23 -> {20, S7, S10, S20, S21, S22, S23, S24, S25, S26, S30, S33, 
   S51, S52, S53, S63, S64, S68, S90, S91, S99}, 
 F27 -> {22, S5, S9, S10, S19, S27, S41, S44, S47, S51, S52, S57, S59,
    S62, S79, S83, S84, S85, S88, S90, S93, S94, S97}|>
$\endgroup$
0
$\begingroup$
x = Sort@Map[Length]@assoc
y = Sort@assoc
Merge[{x, y}, Flatten]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.