• Understanding Data Sorting and Types In Python


    Understanding Data Sorting and Types in Python - Part 1


    People collect all sorts of things. The CDs stacked near your entertainment center, the plates that are part of a series, baseball cards, and even the pens from every restaurant you’ve ever visited are all collections. The collections you encounter when you write applications are the same as the collections in the real world. A collection is simply a grouping of like items in one place and usually organized into some easily understood form.

    This post is about collections of various sorts. The central idea behind every collection is to create an environment in which the collection is properly managed and lets you easily locate precisely what you want at any given time. A set of bookshelves works great for storing books, DVDs, and other sorts of flat items. However, you probably put your pen collection in a holder or even a display case. The difference in storage locations doesn’t change the fact that both house collections. The same is true with computer collections. Yes, there are differences between a stack and a queue, but the main idea is to provide the means to manage data properly and make it easy to access when needed.

     

    Understanding Collections


    A sequence is a succession of values that are bound together in a container. The simplest sequence is a string, which is a succession of characters. Which is a succession of objects. Even though a string and a list are both sequences, they have significant differences. For example, when working with a string, you set all the characters to lowercase — something you can’t do with a list. On the other hand, lists let you append new items, which is something a string doesn’t support. Collections are simply another kind of sequence, albeit a more complex sequence than you find in either a string or list.


    No matter which sequence you use, they all support two functions: index() and count(). The index() function always returns the position of a specified item in the sequence. For example, you can return the position of a character in a string or the position of an object in a list. The count() function returns the number of times a specific item appears in the list. Again, the kind of specific item depends upon the kind of sequence.

    You can use collections to create database-like structures using Python. Each collection type has a different purpose, and you use the various types in specific ways. The important idea to remember is that collections are simply another kind of sequence. As with every other kind of sequence, collections always support the index() and count() functions as part of their base functionality.


    Python is designed to be extensible. However, it does rely on a base set of collections that you can use to create most application types. This post describes the most common collections:

     Tuple: A tuple is a collection used to create complex list-like sequences. An advantage of tuples is that you can nest the content of a tuple. This feature lets you create structures that can hold employee records or x-y coordinate pairs.

     Dictionary: As with the real dictionaries, you create key/value pairs when using the dictionary collection (think of a word and its associated definition). A dictionary provides incredibly fast search times and makes ordering data significantly easier.

     Stack: Most programming languages support stacks directly. However, Python doesn’t support the stack, although there’s a work-around for that. A stack is a first in/first out (FIFO) sequence. Think of a pile of pancakes: You can add new pancakes to the top and also take them off of the top. A stack is an important collection that you can simulate in Python using a list, which is precisely what this post does.

     queue: A queue is a last in/first out (LIFO) collection. You use it to track items that need to be processed in some way. Think of a queue as a line at the bank. You go into the line, wait your turn, and are eventually called to talk with a teller.


     deque: A double-ended queue (deque) is a queue-like structure that lets you add or remove items from either end, but not from the middle. You can use a deque as a queue or a stack or any other kind of collection to which you’re adding and from which you’re removing items in an orderly manner (in contrast to lists, tuples, and dictionaries, which allow randomized access and management).
  • 0 comments:

    Post a Comment

    Powered by Blogger.

    Tags

    Popular Posts