Dictionary data type (other programming languages may be called associated arrays or hash arrays)
Index linked list VS Dictionary (omitted)
Python dictionary
# Initializing an empty dictionary
Pos = {}
# Other dictionary usage: pos. keys0, pos. values (), pos. items ()
# Define a non-empty dictionary
>>>pos= {:, : , : , : >>>pos= dict(colorless=,ideas=, sleep=, furiously=)
The first method is usually used. Note that a dictionary key cannot be modified.
Default dictionary
We can use the default dictionary, so that when accessing a key that does not exist, the default value is assigned, rather than the error message is returned.
Set the default data type:
>>>frequency =>>>frequency[] = 4>>>frequency[>>>pos=>>>pos[]= [, >>>pos[
Set the default value:
>>>pos= nltk.defaultdict(: >>>pos[]= >>>pos[>>>pos.items()
[('blog', 'N'), ('colorless', 'ADJ')]
Incrementally update a dictionary
# Incrementally update the dictionary, sorted by value
>>>counts =>>>>>> (word, tag) brown.tagged_words(categories=+=1>>>counts[22226>>>, , , , , , , , , , , >>> operator >>>sorted(counts.items(), key=itemgetter(1),reverse=, 22226),(, 10845),(, 10648),(, 8336),(, 7313>>>[t t, c sorted(counts.items(), key=itemgetter(1),reverse=, , , , , , , , , , , , ...]
# The Implementation of General accumulated tasks and the simpler method provided by nltk. Index ()
>>>anagrams =>>>= >>>anagrams[, , , , , ]
>>>anagrams = nltk.Index((.join(sorted(w)),w)>>>anagrams[, , , , , ]
Inverted dictionary
>>>pos2= nltk.Index((value, key) (key, value) >>>pos2[, ]
Summary of common methods and dictionary-related habits