"Fluent Python" Data Structures

The second part of the Data Structure

  • Chapter2 An Array of Sequences
  • Chapter3 Dictionaries and Sets
  • Chapter4 Text versus Bytes

 

An Array of Sequences 

This chapter discusses all sequences include list, also discussed Python3 peculiar str and bytes.

Also involved, list, tuples, arrays, queues.

 

Overview of built-in sequence

classification

Container swquences: data container type

  • list, tuple
  • collections.deque: two-way queue.

Flat sequences: only a single type of data stored

  • str,
  • bytes, ByteArray, memoryview  : binary sequence type
  • array.array: array-based array module. A numerical arrays. That is, only store characters, integers, floating point numbers.

Category 2:

Mutable sequences:

  • list, bytearray, array.array
  • collections.deque
  • memoryview

Immutable sequences:tuple, str, bytes

 

 

List Comprehensions and Generator Expressions

It can be abbreviated represent: listcomps, genexps.

Examples: Use inferential list.

#
>>> symbols = '$¢£¥€¤'
>>> codes = [ord(symbol) for symbol in symbols]
>>> codes
[36, 162, 163, 165, 8364, 164]

 

ord (c) is converted to the character corresponding Uicode value.

 

List comprehensions benefits:

  • Than the direct use for statements and more convenient. Also easy to understand.
  • Similar function, will have local scope, there will be no leakage of variables.

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/chentianwei/p/11980932.html