1
$\begingroup$

Analyzing a sequence after restructuring it, Counts returns a sequence similar to below:

assoc = <|{0,1}->8,{1,1}->6,{1,0}->3,{1,-1}->10,{-1,-1}->9,{-1,1}->6,{-1,0}->5,{0,-1}->1|>

I'm only trying to select certain key-values, for example,

keys = {{0, 1}, {1, 0}, {1,-1}, {-1,1}}

Something like KeyTake[assoc, keys] or KeySelect[assoc, keys] is returning empty an association <||>. Forcing the list to be a key isn't working either, such as KeySelect[assoc, Key[{1, 0}]]. I'm fairly certain there is an elegant way to do this in Mathematica. Does anyone have any suggestions?

$\endgroup$
1
  • $\begingroup$ Are you sure that KeyTake[assoc, keys] does not work? It works for me as expect. I am using Mathematica version 12.3.1.0. Maybe this is a version-dependent issue. $\endgroup$ Commented Aug 24, 2021 at 16:17

2 Answers 2

1
$\begingroup$

Excuse me, this was too much for the comment section, but using your own values (and suggestions), it seems to work on 12.2.0 Win7 x64. As I was typing this Henrik Schumacher posted the comment as well. I think you should do: Evaluation: Quit Kernel and try again.

assoc = <|{0, 1} -> 8, {1, 1} -> 6, {1, 0} -> 3, {1, -1} -> 
   10, {-1, -1} -> 9, {-1, 1} -> 6, {-1, 0} -> 5, {0, -1} -> 1|>

<|{0, 1} -> 8, {1, 1} -> 6, {1, 0} -> 3, {1, -1} -> 10, {-1, -1} -> 
  9, {-1, 1} -> 6, {-1, 0} -> 5, {0, -1} -> 1|>

Keys[assoc]

{{0, 1}, {1, 1}, {1, 0}, {1, -1}, {-1, -1}, {-1, 1}, {-1, 0}, {0, -1}}

Values[assoc]

{8, 6, 3, 10, 9, 6, 5, 1}

keys = {{0, 1}, {1, 0}, {1, -1}, {-1, 1}}

KeyTake[assoc, keys]

<|{0, 1} -> 8, {1, 0} -> 3, {1, -1} -> 10, {-1, 1} -> 6|>
$\endgroup$
1
  • $\begingroup$ Bah. Restarting MMA did it. on 12.3. $\endgroup$
    – BBirdsell
    Commented Aug 24, 2021 at 16:36
1
$\begingroup$
assoc = <|{0, 1} -> 8, {1, 1} -> 6, {1, 0} -> 3, {1, -1} -> 
   10, {-1, -1} -> 9, {-1, 1} -> 6, {-1, 0} -> 5, {0, -1} -> 1|>
keys = Key /@ {{0, 1}, {1, 0}, {1, -1}, {-1, 1}}
assoc[[keys]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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