Documentation of dict: A Mathematica Dictionary(Associative Array) using MathOO
This document is a documentation of the dict class in MathOO-packages. Basically it behaviors very similarly to Python dict.
Documentation of dict class
After you load the package, by running:
Get["basic.m"]
you can start to use it.
Generating a new dictionary: new[dict][items___];
namePhone = new[dict][];
generates an empty dictionary.
To add terms when creating the object, do:
namePhone = new[dict][{"john","132452"},{"peter","224235"},{"mary","213414"}];
Items look up: get[key], get[key, default]
In: namePhone.get["john"]
Out: 132452
If the key does not exist, Null will be returned.
In: namePhone.get["john1"] === Null
Out: True
To get a default value instead of Null, supply a second argument.
In: namePhone.get["john1", "Not Found"]
Out: Not Found
Adding new entry/entries: put[key, values], addList[items__]
To add a new entry, do:
namePhone.put["june", "241231"]
To add more than one entry at a time, do:
namePhone.addList[{"tom", "241231"}, {"kings", "241255"}]
Getting all the keys: keys[]
In: namePhone.keys[]
Out: {john, peter, mary, tom, kings, june}
Getting all values: values[]
In: namePhone.values[]
Out: {132452,224235,213414,241231,241255,241231}
Getting all items: items[]
In: namePhone.items[]
Out: {{john,132452},{peter,224235},{mary,213414},{tom,241231},{kings,241255},{june,241231}}
Check if a key exists: hasKey[key]
In: namePhone.hasKey["john"]
Out: True
Getting the number of records: length[]
In: namePhone.length[]
Out: 6
Deleting a single record: delete[key]
namePhone.delete["june"]
Deleting all records: clear[]
namePhone.clear[]
Adding all records from one dictionary to another: update[dict1]
In: namePhoneNew = new[dict][{"stephen","124123"},{"judy","2323421"}];
In: namePhone.update[namePhoneNew];
In: namePhone.items[]
Out: {{john,132452},{peter,224235},{mary,213414},{june,241231},{tom,241231},{kings,241255},{stephen,124123},{judy,2323421}}
Please login to post comment.