[Language Processing and Python] 5.3 use the Python dictionary ing word and Its Attributes

Source: Internet
Author: User
Tags nltk

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.