Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- d = {"foo" : "bar", "spam" : "eggs"}
- print "d has $(d.Count) items"
- //add a new item:
- d["test"] = "new item"
- print "d has ${d.Count} items"
- //change an existing item
- d["foo"] = "barbar"
- //print everything in the dictionary
- for item in d: //item is of type System.Collections.DictionaryEntry
- print item.Key, ":", item.Value
- //an alternative way
- for key in d.Keys:
- print key, "->", d[key]
- if d.ContainsKey("foo"):
- print "has key foo"
- if d.ContainsValue("eggs"):
- print "has value eggs"
- //get the first key: (have to explicitly convert to an array)
- print "first key:", array(d.Keys)[0]
- //print the first value
- print "first value:", array(d.Values)[0]
- //convert hash to a jagged array like python's dictionary.items:
- items = array((item.Key, item.Value) for item in d)
- //(('spam', 'eggs'), ('foo', 'barbar'), ('test', 'new item'))
- //Remove an item:
- d.Remove("test")
- //Getting a default value if the key is not found:
- //This works because d["badkey"] will return null
- item = d["badkey"] or "default value"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement