Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: dictionaries?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20538] Re: [mg20508] Re: dictionaries?
  • From: Daniel Reeves <dreeves at eecs.umich.edu>
  • Date: Sat, 30 Oct 1999 00:13:55 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

since the topic came up, just thought I'd share this (a way to get a list
of the keys for a dictionary):

keys[hash_] :=
  DownValues[hash] /. {RuleDelayed -> (ReleaseHold[#] &), hash -> (# &)}

--    --    --    --    --    --    --    --    --    --    --    -- 
Daniel Reeves               http://ai.eecs.umich.edu/people/dreeves/

"Beware of bugs in the above code; I have only proved it correct, not
tried it."
                -- Donald Knuth

----- FROM Hans Staugaard ON 99.10.27 02:05 (Today) -----

> Joe Strout wrote:
> > 
> > In Python, there's an extremely handy datatype called a "dictionary"
> > which implements a many-to-1 mapping between keys (which can be any
> > hashable type) and values (which can be anything).  E.g.:
> > 
> > > d = {"one":1, "two":2}
> > > d["one"]
> > 1
> > > d["three"]
> > (key not found)
> > 
> > I'm looking for something similar in Mathematica -- it's a great way to
> > organize lists of parameters, without having to worry about the order
> > in which they are listed.
> > 
> > I've searched the online help and flipped through the Mathematica book,
> > but I can't find anything like this.  Have I missed something?
> > 
> > Thanks,
> > -- Joe
> > 
> > --
> > ,------------------------------------------------------------------.
> > |    Joseph J. Strout           Biocomputing -- The Salk Institute |
> > |    joe at strout.net             http://www.strout.net              |
> > `------------------------------------------------------------------'
> > Check out the Mac Web Directory!    http://www.strout.net/macweb.cgi
> 
> You could just say
> 
> 	d["one"]=1;
> 	d["two"]=2;
> 	...
> 
> or if you have a list like this 
> 	
> 	dl={"one",1,"two",2,"three",3}
> 
> you could do like this
> 
> 	Set[d[#1],#2]&@@@Partition[dl,2]
> 
> If you want the function to alert you when there is no key found, you
> could, in both cases, add:
> 
> 	d::nokey="Key not found"
> 	d[_]:=Message[d::nokey]
> 
> Hope that helps
> 
> Hans
> 



  • Prev by Date: CSV Format
  • Next by Date: Simlifying series expansion
  • Previous by thread: Re: dictionaries?
  • Next by thread: boundary conditions