A collection of Python data structures

Collection concept

 

A collection, as its name suggests, is a group of zero or more items that can be treated as a conceptual unit. Almost every significant part of software involves the use of collections. Although some of what we learn in computer science has

 

The change faded away, but the fundamentals of organizing collections did not. Although collections vary in structure and usage, all collections serve the same basic purpose, which is to help programmers organize data efficiently in a program.

 

 

Collections can be viewed from two perspectives. Users and customers of the collection focus on what they can do in various applications. Developers and implementers of collections are concerned with their optimal performance as general-purpose resources.

 

 

Collection type

  • Linear collection

  • Hierarchical collection

  • Collection of graphs

  • unordered collection

  • sorted set

 

Python includes several built-in collection types: strings, lists, tuples, sets, and dictionaries. Strings and lists are probably the most common and basic collection types. Other important collection types include stacks, queues, priority queues, binary search trees, heaps,

 

Graphs, bags, and ordered collections of various types.

 

Collections can be homogeneous, which means that all items in the collection must have the same type, or heterogeneous, which means that the items can be of different types. Although most Python collections can contain multiple types of objects, in many programming languages

 

, the sets are isomorphic.

 

Collections are usually dynamic rather than static, which means that they can grow or shrink as the problem demands. Furthermore, their content can be changed throughout the course of the program. An exception to this rule are immutable collections, such as Python strings

 

and tuples. Items of an immutable collection are added during its creation, after which items cannot be added, removed, or replaced.

 

Another important feature that distinguishes sets is that they are organized differently. The following describes the usage of several broad categories of sets, including linear sets, hierarchical sets, graph sets, unordered sets, and ordered sets.

 

 

Linear collection

 

 

Items in a linear set are like people in a queue, ordered by position. Every item except 1 has a unique predecessor, and every item except the last has a unique successor. The predecessor of D2 is D1, and the successor is D3, as shown in the figure

Examples of linear sets include a shopping list, a stack of dinner plates, and a line of customers waiting at an ATM

 

 

Hierarchical collection

 

 

The data items in a hierarchical collection are ordered in a structure similar to an upside-down tree. Except for the first data item at the very top, each data item has only one predecessor, called its parent, but may have multiple successors, called its children. The precursor (father) of the D3 is

 

D1, whose successors (children) are D4, D5 and D6

 

 

 

A file system, a company's organizational tree, and a book's table of contents are all examples of hierarchical collections

 

 

Collection of graphs

 

 

A set of graphs is also called a graph, and each item in this set may have multiple predecessors and multiple successors. As shown in the figure below, all elements connected to D3 are considered to be its predecessors and successors, and they are also called its neighbors

 

 

 

Diagrams of routes between cities and diagrams of electrical connections between buildings are examples of diagrams

 

 

unordered collection

 

 

As the name suggests, the items in an unordered collection have no particular order, and it doesn't make sense to discuss an item's predecessor or successor. The figure below shows such a structure.

 

 

A bag of glass balls is an example of an unordered set. There is no specific order for the glass balls, although you can put the glass balls into the bag and remove the glass balls from the bag in any order you want

 

 

sorted set

 

 

Sorted sets impose a natural ordering on their items. Examples are entries in a phone book (various paper versions of the 20th century) and entries in a class roster .

 

In order to impose a natural order, there must be some kind of rule for comparing items, for example, itemi<=itemi+1, in order to be able to access the items in the sorted set.

 

Sorted lists are the most common example of sorted sets, which do not need to be linear or ordered by position. From the client's perspective, sets, bags, and dictionaries may all be ordered, even if their items are not accessed by position. Hierarchical collection

 

A special type of is called a binary search tree, which also imposes a natural ordering on its entries.

 

An ordered collection that allows clients to access all of its items in sorted order. Some operations, such as searching, may also be more efficient on sorted sets than on unordered sets.

 

 

Classification of collection types

 

 

Remembering the main types of collections, we can now categorize the different common collection types as shown in the image below. This taxonomy will help us to represent these types of Python classes.

 

 

Classification of collection types

 

 

Note that the type names in this category do not imply a particular implementation of a collection, and there may be multiple implementations of a particular type of collection. Also, some of these names, such as "set" and "linear set", specify a classification of set types rather than a special

 

collection type. However, these classifications are useful for organizing the characteristics and behaviors of certain types of collections that share similarities.

 

 

Classification of operations on sets

Operation classification description

Determine the size Use Python's len function to get the current number of items in a collection

The membership of test items uses Python's in operator to search the collection for a given target item. Returns True if the item is found; otherwise, returns False

Iterating over a collection uses Python's for loop to access each item in the collection. The order in which the items are accessed depends on the type of the collection

Get a String Representation Use Python's str function to get the string representation of a collection

Testing for equality uses Python's == operator to determine whether two sets are equal. Two sets are equal if they have the same type and contain the same items. The order of which two pairs of items are compared depends on the type of the collection

Concatenate two sets using Python's + operator to obtain a new set of the same type as the operands, and the new set contains the items in both operands

Converting to another type of collection creates a new collection using the same items in the source collection. Cloning is a special case of type conversion, where two collections have the same type

Insert an item to add an item to the collection, possibly at a given position

delete item removes an item from the collection, possibly at a given position

Replace an item combines deletion and insertion into one operation

access or get an item get an item, possibly from a given location

Note that several of these operations are related to standard Python operators, functions, or control statements, such as in, +, len, str, and for loops. We are already familiar with how to use them with Python strings and lists.

There are no separate names for insert, delete, replace, or access operations in Python. However, there are some standard variants. For example, the pop method is used to remove an item from a given position in a Python list, or to remove the value of a given key from a Python dictionary.

The remove method is used to remove the given item from a Python set or list. As new collection types are developed that are not yet supported in Python, we will do our best to use standard operator, function, or method names for their operations.

One set operation that you may not be familiar with is type conversion. We have seen the use of type conversions when entering numbers. In this environment, we receive a string of numbers from the keyboard and apply an int or float function to the input string to convert it to an integer

and floating point numbers

A collection of one type can be converted to a collection of another type in a similar manner. For example, Python strings can be converted to Python lists, and Python lists can be converted to Python tuples, as shown in the following code:

1 message = " Hi there! "  
2 lyst = list(message) 
 3  lyst
 4  ['H', 'i', '', 't', 'h', 'e', ​​'r', 'e', '!'] 
 5  toople tuple(lyst) 
 6  toople
 7  ('H', 'i', '', 't', 'h', 'e', ​​'r', 'e', ​​'!')
 8  The parameter of the list or tuple function does not need to be another collection, it can be any iterable object. Iterable objects allow programmers to use Python's for loop to access a sequence of items (yes, that sounds like a collection, and all collections are also iterables). For example, we can use a range function to create a list as follows:
 9 lyst = list(range(1, 11, 2 ))
 10  lyst
 11 [1, 3, 5, 7, 9 ]
 12 other functions, Like the dict function for dictionaries, it expects an iterable of a more specific type as its argument, such as a list of tuples of (key, value).

 

Normally, if the argument is omitted, the type conversion function of a collection returns a new, empty collection of that type.

Cloning is a special type of cast that returns an exact copy of the argument to the cast function. In this case, the type of the parameter and the type of the conversion function should be the same. For example, the following code snippet makes a copy of the list and then uses the is and == operators to compare the two lists. is returns False if the two lists are not the same object. Although the two lists are different objects, == returns True because they have the same type and have the same result (the same pair of elements at every position in the two lists).

 

1 lyst1 = [2, 4, 8 ] 
 2 lyst2 = list(lyst1) 
 3 lyst1 is lyst2 
 4  False
 5 lyst1 == lyst2
 6  True
 7 Not only do the two lists in this example have the same structure, they also have the same item. That is, the list function makes a shallow copy of its argument list, and the items don't make a copy of themselves before being added to the new list.

 

  

When the items are immutable (numbers, strings, or Python tuples), this strategy is fine. However, when collections share mutable items, side effects can occur. To prevent this from happening, the programmer can create a deep copy by writing a for loop on the source collection, which explicitly copies the items of the new collection before adding it to the new collection.

Subsequent chapters will take the strategy of providing type conversion functions for most collection types. This function accepts an iterable object as an optional argument and performs a shallow copy of the items accessed.

Implementation of collections

In fact, the programmers responsible for writing programs that use collections have a different perspective on these collections than the programmers who were originally responsible for implementing the collections.

Programmers working with collections need to know how to instantiate and use each collection type. From their perspective, a collection is a means of storing and accessing data items in some predefined way, and they don't care about the details of the collection's implementation. in other words,

From the user's point of view, a collection is an abstraction, and for this reason, in computer science, collections are also called abstract data types (ADTs). The user of ADT only cares about its interface, or what the object of that type knows

another set of operations.

Collection developers, on the other hand, are concerned with implementing the collection's behavior in the most efficient way possible, with the goal of providing the best performance for the collection's users. Often there may be a large number of implementations. However, many implementations take up too much space,

Or run so slowly that we don't think it's worth it. The remaining implementations tend to organize and access computer memory based on a few basic methods. Chapters 3 and 4 describe these methods in detail.

Some programming languages, such as Python, provide only one implementation for each available collection type. Other languages, like Java, offer several implementations. For example, Java's java.util package contains two implementations of lists, ArrayList and

LinkedList. There are also two implementations of sets and maps (just like Python's dictionaries), namely HashSet, TreeSet, HashMap, and TreeMap. Java programmers use the same interface (set of operations) for each implementation,

However, one can freely choose from among the various implementations based on their performance characteristics and other criteria. A major goal of this book is to give Python programmers the same options as Java programmers. The book also introduces abstract data classes

types, and indicate their implementations that are not available in either language. For each type of set (linear, hierarchical, graph, unordered and ordered). You'll see one or more abstract collection types, and one or more implementations.

The idea of ​​abstraction is not unique when discussing collections. Abstraction is an important principle in many places, both within and outside of computer science. For example, when learning about the effects of gravity on falling objects, you might try to build a real

Test conditions, where you can ignore insignificant details like the color and taste of objects (eg, the apple that hit Newton's head). When learning math, you don't need to care what numbers are used to count fish hooks or arrowheads, but try to find

Principles of abstraction and permanence of numbers. The interior design plan of the house is an abstraction of the physical house, which allows you to focus on the structural elements without being overwhelmed by insignificant details such as the color of the cabinets. Structural elements are those

A detail that is important to the overall appearance of a house and does not involve the relationship between the main components of the house.

In computer science, abstraction is used to ignore or hide those unimportant details. Software systems are usually built layer by layer, each layer being treated as an abstraction by the layer above it that uses it. Without abstraction, you will need to think about software as well

In all aspects of design, this is an impossible task. Of course, you eventually have to think about the details, but, you can do these things in a smaller, more manageable environment.

In Python, functions and methods are the smallest unit of abstraction, classes are the next larger unit, and modules are the largest unit

summary

  • Collections are objects that hold 0 or more other objects. Collections have operations to access objects, insert objects, delete objects, determine the size of the collection, and traverse or access the objects of the collection.
  • The 5 main categories of sets are: linear sets, hierarchical sets, graph sets, unordered sets, and ordered sets.
  • A linear set arranges its items by position, except for the first item, each item has a unique predecessor, and each item except the last item has a unique successor.
  • Items in a hierarchical collection have a unique predecessor (with one exception, the item at the top level), and zero or more successors. A single item called the root has no predecessor.
  • Items of a graph collection have 0 or more successors, and 0 or more predecessors.
  • The items in an unordered collection do not have a specific order.
  • Collections are iterable, and a for loop can be used to access each item contained in the collection.
  • An abstract data type is a set of objects, and operations on those objects. Therefore, collections are abstract data types.
  • A data structure is an object that represents the data contained in a collection.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860463&siteId=291194637