数据结构( Pyhon 语言描述 ) — —第9章:列表

Posted lijunjie9502

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构( Pyhon 语言描述 ) — —第9章:列表相关的知识,希望对你有一定的参考价值。

    1. 概念
      • 列表是一个线性的集合,允许用户在任意位置插入、删除、访问和替换元素
    2. 使用列表
      • 基于索引的操作
        • 基本操作
          • 技术分享图片
        • 数组与列表的区别
          • 数组是一种具体的数据结构,拥有基于单个的物理内存块的一种特定的,不变的实现。
          • 列表是一种抽象的数据类型,可以由各种方式表示,数组只是其中一种方式
      • 基于内容的操作
        • 基本操作
          • 技术分享图片
      • 基于位置的操作
        • 相对于游标位置执行,这个操作允许程序员在通过移动游标在列表中导航。其可通过列表迭代器来实现
        • 列表迭代器是附加到列表的后备储存
          • 技术分享图片
        • 列表迭代器游标的位置
          • 第一项之前
          • 相领两项之间
          • 最后一项之后
          • 列表迭代器第一次实例化的时候,其游标是未定义的
        • 列表迭代器的方法
          • 导航操作
            • 技术分享图片
          • 修改操作
            • 技术分享图片
            • remove replace 操作均需要满足先验条件
      • 列表的接口
        • 基本列表操作概览
          • 技术分享图片
        • 列表和列表迭代器的接口和实现类
          • 技术分享图片
    3. 列表的应用
      • 列表可能是计算机领域应用最为广泛的集合
      • 堆存储管理
        • 在一个可用的列表中,将堆上可用空间的连续块链接起来。当应用程序需要一个新的实例化对象时,PVM会在可用列表中搜索大小足够容纳对象的第一个块。当不再需要对象的时候,垃圾收集程序会把对象的空间返回给可用列表
        • 为了减少搜索时间,可以使用多个可用列表,分别包含不同字节大小的块;最后一个列表应该包含比某个指定的大小还要大的所有的块。
      • 组织磁盘上的文件
        • 计算机文件系统有3个主要的部分:文件的目录、文件自身以及可用空间。
        • 磁盘的物理格式
          • 磁盘的表面被划分为多个同心圆磁道,每个磁道被进一步划分为扇区。所有磁道包含的扇区数相同,所有扇区包含的字节数也相同
          • 示意图
            • 技术分享图片
        • 文件系统
          • 假设目录占用了磁盘上的前几个磁道并且为每个文件包含了一个条目。这个条目包含文件的信息和扇区地址,这个扇区地址指向文件中的第 1 个字节
          • 根据文件的大小,其可能完全包含在一个扇区,或者同时跨越几个扇区。
          • 组成文件的扇区并不需要物理相邻,扇区的最后有一个指针,该指针指向了包含文件的下一个扇区
        • 列表在文件系统中的应用
          • 没有使用的扇区在一个可用列表中被链接起来。当创建新文件时,它们从这个列表中得到分配的空间,当删除旧文件时,它们的空间释放到这个列表中
        • 磁盘读写步骤
          • /写头定位到正确的磁道
          • 磁盘转动,直到目标扇区在读写头下
          • 进行数据读写
          • 3个步骤中,数据读写所需要时间最少
        • 性能优化
          • 根据磁盘的特性,当跨多个扇区的文件,没有在磁盘上分散开时,磁盘的性能会得到优化
          • 文件系统中包含一个工具,它会重新组织文件系统以便每个文件中的扇区都是连续的,并且具有相同的物理顺序和逻辑顺序。从而优化磁盘的性能。
      • 其他集合的实现
        • 列表可用来实现其它集合,通常有两种方法
          • 扩展列表类,让新的类成为列表类的一个子类;
          • 在新的类中,使用列表类的一个实例,并且让该列表包含数据项
    4. 列表实现
      • AbstractList 类的角色
        • 列表迭代器会使用 self._modCount 这个实例变量在某些方法上强制施加先验条件
        • 代码实现
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            from abstractcollection import AbstractCollection

             

             

            class AbstractList(AbstractCollection):

                """An abstract list implementation."""

             

                def __init__(self, sourceCollection):

                    """Maintains a count of modifications to the left."""

                    self._modCount = 0

                    AbstractCollection.__init__(self, sourceCollection)

             

                def getModCount(self):

                    """Returns the count of modifications to the list."""

                    return self._modCount

             

                def incModCount(self):

                    """Increment the count of modifications."""

                    self._modCount += 1

             

                def index(self, item):

                    """Precondition: the item is in the list.

                    Return the position of the item.

                    Raise: ValueError if item isn‘t in the list"""

                    position = 0

                    for data in self:

                        if data == item:

                            return position

                        else:

                            position += 1

                    if position == len(self):

                        raise ValueError(str(item) + " not in list.")

             

                def add(self, item):

                    """Add the item to the end of the list."""

                    self.insert(len(self), item)

             

                def remove(self, item):

                    """Precondition: the item is in the list.

                    Raise: ValueError is item isn‘t in the list.

                    PostCondition:item is removed from self."""

                    position = self.index(item)

                    self.pop(position)

      • 基于数组的实现
        • ArrayList 有一个初始的默认大小,当需要时,其大小会自动变化
        • 代码实现
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            """

            File: ArrayList.py

            """

             

            from arrays import Array

            from abstractlist import  AbstractList

            from arraylistiterator import ArrayListIterator

             

             

            class ArrayList(AbstractList):

                """An array-based list implementation."""

             

                # Class variable

                DEFAULT_CAPACITY = 10

             

                def __init__(self, sourceCollection=None):

                    """Sets the initial state of self, which includes the contents

                       of sourceCollection, if it‘s present."""

                    self._items = Array(ArrayList.DEFAULT_CAPACITY)

                    AbstractList.__init__(self, sourceCollection)

             

                # Accessor method

                def __iter__(self):

                    """Support iteration over a view of self."""

                    cursor = 0

                    while cursor < len(self):

                        yield self._items[cursor]

                        cursor += 1

             

                def __getitem__(self, i):

                    """Precondition: 0<= i < len(self)

                    Return the item at position i.

                    Raises: IndexError"""

             

                    if i < 0 or i >= len(self):

                        raise IndexError("List out of range.")

                    return self._items[i]

             

                # Mutator method

                def __setitem__(self, i, item):

                    """Precondition: 0 <= i < len(self)

                    Replace the item at position i.

                    Raise: IndexError"""

                    if i < 0 or i >= len(self):

                        raise IndexError("List out of range.")

                    self._items[i] = item

             

                def insert(self, i, item):

                    """Inserts item at the position i."""

                    # Resize the array if necessary

                    self._growArray()

                    # Set the position i between 0 and len(self)

                    if i < 0: i = 0

                    elif i > len(self): i = len(self)

                    if i < len(self):

                        for index in range(len(self), i, -1):

                            self._items[index] = self._items[index - 1]

                    self._items[i] = item

                    self._size += 1

                    self.incModCount()

             

                def pop(self, i=None):

                    """Precondition: 0 <= i < len(self).

                    Remove and return item at position i. if i is None, i is given a default of len(self) - 1"

                    Raise: Index Error."""

                    if i is None: i = len(self) - 1

                    if i < 0 or i >= len(self):

                        raise IndexError("List index out of range.")

                    item = self._items[i]

                    for index in range(i, len(self) - 1):

                        self._items[index] = self._items[index + 1]

                    self._items[len(self) - 1] = None

                    self._size -= 1

                    self.incModCount()

                    # Resize array in necessary

                    self._shrinkArray()

                    return item

             

                def clear(self):

                    """Clear the array list."""

                    self._items = Array(ArrayList.DEFAULT_CAPACITY)

                    self._size = 0

                    self.incModCount()

             

                def listIterator(self):

                    """Returns a list iterator."""

                    return ArrayListIterator(self)

             

                def _growArray(self):

                    """Grow the capacity of the array if necessary """

                    physicalSize = len(self._items)

                    if len(self) >= physicalSize:

                        temp = Array(physicalSize * 2)

                        index = 0

                        for item in self:

                            temp[index] = item

                            index += 1

                        self._items = temp

             

                def _shrinkArray(self):

                    """Shrink the capacity of the array if necessary. """

                    physicalSize = len(self._items)

                    if len(self) <= physicalSize // 4 and physicalSize >= 2 * ArrayList.DEFAULT_CAPACITY:

                        temp = Array(physicalSize // 2)

                        index = 0

                        for item in self:

                            temp[index] = item

                            index += 1

                        self._items = temp

      • 链表实现
        • 采用包含一个哨兵节点的双链表结构
          • 示意图
            • 技术分享图片
          • 优点
            • 不需要一个单独的尾指针
            • 当插入或删除第一个或最后一个节点的时候,不需要重新设置头指针
        • _getNode(i) 辅助方法用来搜索第 i 个节点
        • 代码实现
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            from node import TwoWayNode

            from abstractlist import AbstractList

            from linkedlistiterator import LinkedListIterator

             

             

            class LinkedList(AbstractList):

                """A link-based list implementation"""

             

                def __init__(self, sourceCollection=None):

                    """Sets the initial state of self, which includes the contents

                       of sourceCollection, if it‘s present."""

                    self._head = TwoWayNode(None)

                    self._head.previous = self._head.next = self._head

                    AbstractList.__init__(self, sourceCollection)

             

                # Helper method returns node at position i

                def _getNode(self, i):

                    """Helper method: returns a pointer to node at position i."""

                    if i == len(self):  # Constant-time access to head node.

                        return self._head

                    if i == len(self) - 1: # or last data node

                        return self._head.previous

                    probe = self._head.next

                    while i > 0:

                        probe = probe.next

                        i -= 1

                    return probe

             

                # Accessor method

                def __iter__(self):

                    """Support iteration over a view of self."""

                    cursor = self._head.next

                    while cursor != self._head:

                        yield cursor.data

                        cursor = cursor.next

             

                def __getitem__(self, i):

                    """Precondition: 0<= i < len(self)

                    Return the item at position i.

                    Raises: IndexError"""

             

                    if i < 0 or i >= len(self):

                        raise IndexError("List out of range.")

                    return self._getNode(i).data

             

                def getHead(self):

                    return self._head

             

                # Mutator method

                def __setitem__(self, i, item):

                    """Precondition: 0 <= i < len(self)

                    Replace the item at position i.

                    Raise: IndexError"""

                    if i < 0 or i >= len(self):

                        raise IndexError("List out of range.")

                    self._getNode(i).data = item

             

                def insert(self, i, item):

                    """Inserts item at the position i."""

                    # Set the position i between 0 and len(self)

                    if i < 0: i = 0

                    elif i > len(self): i = len(self)

                    theNode = self._getNode(i)

                    newNode = TwoWayNode(item, theNode.previous, theNode)

                    theNode.previous.next = newNode

                    theNode.previous = newNode

                    self._size += 1

                    self.incModCount()

             

                def pop(self, i=None):

                    """Precondition: 0 <= i < len(self).

                    Remove and return item at position i. if i is None, i is given a default of len(self) - 1"

                    Raise: Index Error."""

                    if i is None: i = len(self) - 1

                    if i < 0 or i >= len(self):

                        raise IndexError("List index out of range.")

                    theNode = self._getNode(i)

                    item = theNode.data

                    theNode.previous.next = theNode.next

                    theNode.next.previous = theNode.previous

                    self._size -= 1

                    self.incModCount()

                    return item

             

                def clear(self):

                    """Clear the linked list."""

                    self._head.previous = self._head.next = self._head

                    self._size = 0

                    self.incModCount()

             

                def listIterator(self):

                    """Returns a list iterator."""

                    return LinkedListIterator(self)

      • 两种实现的时间和空间分析
        • 列表操作的平均运行时间
          • 技术分享图片
        • 空间分析
          • 当数组的装载因子大于 1/3 时,其空间效率要比列表高,否则要低
    5. 实现列表迭代器
      • 列表迭代器的角色和作用
        • 列表迭代器对象依赖于相关联的列表,因为前者需要访问后者以定位项、替换项、插入项和删除项
        • 3种类型的先验条件
          • 如果hasNext hasPrevious 分别返回 False,程序员不能运行 next previous 操作
          • 不能在列表迭代器上运行连续性的修改器方法。在每次运行前,必须先运行一个 next previous 以建立游标位置
            • 使用实例变量 self._lastItemPos 来判断,当位置未确定时,其值为 -1。当程序在列表迭代器上成功运行一个next previous 操作时,其值变成了列表中的一个索引。
            • 列表迭代器中的修改器方法,通过检查 self._lastItemPos 以确保满足先验条件,并在成功修改列表后,将其重置为 -1
          • 在列表上使用列表迭代器方法时,程序不能使用列表修改器方法在列表自身之上进行修改(如果修改会影响列表的游标位置,__setitem__方法不会影响)
            • 通过比较列表的 modCount 和列表迭代器的 modCount 是否相同来判断
      • 数组实现
        • 代码
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            """

            Filearraylistiterator.py

            """

             

             

            class ArrayListIterator(object):

                """Represents the list iterator for an array list."""

             

                def __init__(self, backingStore):

                    """Set the initial state of the list iterator."""

                    self._backingStore = backingStore

                    self._modCount = backingStore.getModCount()

                    self._cursor = 0

                    self._lastItemPos = -1

                    self.first()

             

                def first(self):

                    """Resets the cursor to the beginning of the backing store."""

                    self._cursor = 0

                    self._lastItemPos = -1

             

                def last(self):

                    """Moves the cursor to the end of the backing store."""

                    self._cursor = len(self._backingStore)

                    self._lastItemPos = -1

             

                def hasNext(self):

                    """Return True if the iterator has a next item or False otherwise."""

                    return self._cursor < len(self._backingStore)

             

                def next(self):

                    """Precondition: hasNext returns True.The list has not been modified except by this iterator‘s mutator.

                    Returns the current item and advances the cursor to the next item."""

                    if not self.hasNext():

                        raise ValueError("No next item in list iterator.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    self._lastItemPos = self._cursor

                    self._cursor += 1

                    return self._backingStore[self._lastItemPos]

             

                def hasPrevious(self):

                    """Return True if the iterator has a previous item or False otherwise"""

                    return self._cursor > 0

             

                def previous(self):

                    """Precondition: hasPrevious returns True. The list has not been modified except by this iterator‘s mutator.

                    """

                    if not self.hasPrevious():

                        raise ValueError("No previous item in list iterator.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    self._cursor -= 1

                    self._lastItemPos = self._cursor

                    return self._backingStore[self._lastItemPos]

             

                def replace(self, item):

                    """Precondition: the current position is defined. The list has not been modified except by this iterator‘s

                    mutator."""

                    if self._lastItemPos == -1:

                        raise AttributeError("The current position is undefined.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    self._backingStore[self._lastItemPos] = item

                    self._lastItemPos = -1

             

                def insert(self, item):

                    """Precondition: The list has not been modified except by this iterator‘s mutator."""

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    if self._lastItemPos == -1:

                        # Cursor not defined, so add item to end of list.

                        self._backingStore.add(item)

                    else:

                        self._backingStore.insert(self._lastItemPos, item)

                    # If the item insert was obtained via next, move cursor forward

                    if self._lastItemPos < self._cursor:

                        self._cursor += 1

                    self._lastItemPos = -1

                    self._modCount += 1

             

                def remove(self):

                    """Precondition: the current position is defined. The list has not been modified except by this iterator‘s

                    mutator."""

                    if self._lastItemPos == -1:

                        raise AttributeError("The current position is undefined.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("List has been modified illegally.")

                    self._backingStore.pop(self._lastItemPos)

                    # If the item removed was obtained via next, move cursor back

                    if self._lastItemPos < self._cursor:

                        self._cursor -= 1

                    self._modCount += 1

                    self._lastItemPos = -1

      • 链表实现
        • 用后备储存链表结构中的节点来表示游标,通过将游标设置为下一个节点或者前一个节点来实现导航性方法。这些会使得可能的修改操作都成为常数时间
        • 代码
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            """

            Filelinkedlistiterator.py

            """

             

            from node import TwoWayNode

             

             

            class LinkedListIterator(object):

                """Represents the list iterator for an linked list."""

             

                def __init__(self, backingStore):

                    """Set the initial state of the list iterator."""

                    self._backingStore = backingStore

                    self._modCount = backingStore.getModCount()

                    self._head = backingStore.getHead()

                    self._cursor = self._head

                    self._lastItemPos = None

                    self.first()

             

                def first(self):

                    """Resets the cursor to the beginning of the backing store."""

                    self._cursor = self._head.next

                    self._lastItemPos = None

             

                def last(self):

                    """Moves the cursor to the end of the backing store."""

                    self._cursor = self._head.previous

                    self._lastItemPos = None

             

                def hasNext(self):

                    """Return True if the iterator has a next item or False otherwise."""

                    return self._cursor.next != self._head

             

                def next(self):

                    """Precondition: hasNext returns True.The list has not been modified except by this iterator‘s mutator.

                    Returns the current item and advances the cursor to the next item."""

                    if not self.hasNext():

                        raise ValueError("No next item in list iterator.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    self._lastItemPos = self._cursor

                    self._cursor = self._cursor.next

                    return self._lastItemPos.data

             

                def hasPrevious(self):

                    """Return True if the iterator has a previous item or False otherwise"""

                    return self._cursor.previous != self._head

             

                def previous(self):

                    """Precondition: hasPrevious returns True. The list has not been modified except by this iterator‘s mutator.

                    """

                    if not self.hasPrevious():

                        raise ValueError("No previous item in list iterator.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    self._lastItemPos = self._cursor

                    self._cursor = self._cursor.previous

                    return self._lastItemPos.data

             

                def replace(self, item):

                    """Precondition: the current position is defined. The list has not been modified except by this iterator‘s

                    mutator."""

                    if self._lastItemPos is None:

                        raise AttributeError("The current position is undefined.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    self._lastItemPos.data = item

                    self._lastItemPos = None

             

                def insert(self, item):

                    """Precondition: The list has not been modified except by this iterator‘s mutator."""

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("Illegal modification of backing store.")

                    if self._lastItemPos is None:

                        # Cursor not defined, so add item to end of list.

                        self._backingStore.add(item)

                    else:

                        newNode = TwoWayNode(item, self._lastItemPos.previous, self._lastItemPos)

                        self._lastItemPos.previous.next = newNode

                        self._lastItemPos.previous = newNode

                        self._backingStore.incModCount()

                    self._lastItemPos = None

                    self._modCount += 1

             

                def remove(self):

                    """Precondition: the current position is defined. The list has not been modified except by this iterator‘s

                    mutator."""

                    if self._lastItemPos is None:

                        raise AttributeError("The current position is undefined.")

                    if self._modCount != self._backingStore.getModCount():

                        raise AttributeError("List has been modified illegally.")

                    self._lastItemPos.previous.next = self._lastItemPos.next

                    self._lastItemPos.next.previous = self._lastItemPos.previous

                    self._backingStore.incModCount()

                    self._modCount += 1

                    self._lastItemPos = None

      • 列表迭代器的时间和空间分析
        • 链表实现,所有的运行方法都是O(1),数组实现的 insert remove 方法都是 O(n)
    1. 开发一个有序的列表
      • 分析
        • 与常规列表的对比
          • 不能使用 insert __setitem__ 方法
          • add 方法会在已有项之间,查找插入该项的一个合适的位置
          • index 操作,会利用列表是有序的这一事实,针对给定项执行一次二叉搜索
      • 设计
        • 为了支持二叉搜索,因此选用基于数组实现的列表
        • ArraySortedList 不能是 ArrayList 的一个子类,因为这样 ArraySortedList 会继承两个不能使用的方法
          • 关系图
            • 技术分享图片
        • 使用 ArrayList 的一个实例
          • 使用 ArrayList 的一个实例来使 ArraySortedList 作为类的容器而子类化。但是如此并没有实现代码复用,ArrayList 中的每个方法,在 ArraySortedList 中都必须重新定义
          • 实现策略
            • 技术分享图片
        • ArrayList 作为 ArraySortedList 的一个子类
          • 列表实际上是带有两个额外方法( insert __setitem__ )的有序列表,即列表接口是有序列表接口的一个扩展。
          • 实现策略
            • 技术分享图片
          • 列表方法分布
            • 技术分享图片
            • ArrayList 中的 __contains__ 方法也需要重写
          • 迭代器方法可以使用类似的方法实现
            • ArraySortedListIterator 中包含了所有的导航方法以及 remove 方法。其子类 ArrayListIterator 中则只包含 insert replace 方法
      • 实现
        • ArraySortedList
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            """

            File: arraysortedlist.py

            """

             

            from arrays import Array

            from abstractlist import AbstractList

            from arraysortedlistiterator import ArraySortedListIterator

             

             

            class ArraySortedList(AbstractList):

                """An array-based sorted list implementation."""

             

                # Class variable

                DEFAULT_CAPACITY = 10

             

                def __init__(self, sourceCollection=None):

                    """Sets the initial state of self, which includes the contents

                       of sourceCollection, if it‘s present."""

                    self._items = Array(ArraySortedList.DEFAULT_CAPACITY)

                    AbstractList.__init__(self, sourceCollection)

             

                # Accessor method

                def __iter__(self):

                    """Support iteration over a view of self."""

                    cursor = 0

                    while cursor < len(self):

                        yield self._items[cursor]

                        cursor += 1

             

                def __getitem__(self, i):

                    """Precondition: 0<= i < len(self)

                    Return the item at position i.

                    Raises: IndexError"""

             

                    if i < 0 or i >= len(self):

                        raise IndexError("List out of range.")

                    return self._items[i]

             

                def __contains__(self, item):

                    position = self._binarySearch(item)

                    if self._items[position] == item:

                        return True

                    else:

                        return False

             

                def _binarySearch(self, item):

                    """Search item in the array sorted list."""

                    left = 0

                    right = len(self) - 1

                    mid = len(self)

                    while left <= right:

                        mid = (left + right) // 2

                        if self._items[mid] == item:

                            break

                        if self._items[mid] > item:

                            right = mid - 1

                        else:

                            left = mid + 1

                    return mid

             

                def index(self, item):

                    """Precondition: the item is in the list.

                    Return the position of the item.

                    Raise: ValueError if item isn‘t in the list"""

                    position = self._binarySearch(item)

                    if self._items[position] != item:

                        raise ValueError(str(item) + " isn‘t in the list.")

                    # find the first position if there is multiply items

                    for i in range(position - 1, -1, -1):

                        if self._items[i] != item:

                            return i + 1

                    # return the first place

                    return 0

             

                # Mutator method

                def pop(self, i=None):

                    """Precondition: 0 <= i < len(self).

                    Remove and return item at position i. if i is None, i is given a default of len(self) - 1"

                    Raise: Index Error."""

                    if i is None: i = len(self) - 1

                    if i < 0 or i >= len(self):

                        raise IndexError("List index out of range.")

                    item = self._items[i]

                    for index in range(i, len(self) - 1):

                        self._items[index] = self._items[index + 1]

                    self._items[len(self) - 1] = None

                    self._size -= 1

                    self.incModCount()

                    # Resize array in necessary

                    self._shrinkArray()

                    return item

             

                def add(self, item):

                    """Add the item to the proper position of the list."""

                    # Resize the array if necessary.

                    self._growArray()

                    # Find the position by binary search

                    position = self._binarySearch(item)

                    flag = True

                    if position < len(self) and self._items[position] != item:

                        if item > self._items[position]:

                            for i in range(position + 1, len(self)):

                                if item <= self._items[i]:

                                    position = i

                                    flag = False

                                    break

                            if flag:

                                position = len(self)

                        else:

                            for i in range(position - 1, -1, -1):

                                if item >= self._items[i]:

                                    position = i + 1

                                    flag = False

                                    break

                            if flag:

                                position = 0

                    # Insert item at the proper position.

                    if position < len(self):

                        for index in range(len(self), position, -1):

                            self._items[index] = self._items[index - 1]

                    self._items[position] = item

                    self._size += 1

                    self.incModCount()

             

                def clear(self):

                    """Clear the array list."""

                    self._items = Array(ArraySortedList.DEFAULT_CAPACITY)

                    self._size = 0

                    self.incModCount()

             

                def listIterator(self):

                    """Returns a list iterator."""

                    return ArraySortedListIterator(self)

             

                def _growArray(self):

                    """Grow the capacity of the array if necessary """

                    physicalSize = len(self._items)

                    if len(self) >= physicalSize:

                        temp = Array(physicalSize * 2)

                        index = 0

                        for item in self:

                            temp[index] = item

                            index += 1

                        self._items = temp

             

                def _shrinkArray(self):

                    """Shrink the capacity of the array if necessary. """

                    physicalSize = len(self._items)

                    if len(self) <= physicalSize // 4 and physicalSize >= 2 * ArraySortedList.DEFAULT_CAPACITY:

                        temp = Array(physicalSize // 2)

                        index = 0

                        for item in self:

                            temp[index] = item

                            index += 1

                        self._items = temp

        • ArrayList
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            """

            File: ArrayList.py

            """

             

            from abstractlist import AbstractList

            from arraysortedlist import ArraySortedList

            from arraylistiterator import ArrayListIterator

             

             

            class ArrayList(ArraySortedList):

                """An array-based list implementation."""

             

                def __init__(self, sourceCollection=None):

                    """Sets the initial state of self, which includes the contents

                       of sourceCollection, if it‘s present."""

                    ArraySortedList.__init__(self, sourceCollection)

             

                # Accessor method

                def index(self, item):

                    """Precondition: the item is in the list.

                    Return the position of the item.

                    Raise: ValueError if item isn‘t in the list"""

                    AbstractList.index(self, item)

             

                def __contains__(self, item):

                    for i in self:

                        if i == item:

                            return True

                    return False

             

                # Mutator method

                def __setitem__(self, i, item):

                    """Precondition: 0 <= i < len(self)

                    Replace the item at position i.

                    Raise: IndexError"""

                    if i < 0 or i >= len(self):

                        raise IndexError("List out of range.")

                    self._items[i] = item

             

                def insert(self, i, item):

                    """Inserts item at the position i."""

                    # Resize the array if necessary

                    self._growArray()

                    # Set the position i between 0 and len(self)

                    if i < 0: i = 0

                    elif i > len(self): i = len(self)

                    if i < len(self):

                        for index in range(len(self), i, -1):

                            self._items[index] = self._items[index - 1]

                    self._items[i] = item

                    self._size += 1

                    self.incModCount()

             

                def add(self, item):

                    """Add the item to the end of the list."""

                    AbstractList.add(self, item)

             

                def listIterator(self):

                    """Returns a list iterator."""

                    return ArrayListIterator(self) 

     

    • ArraySortedListIterator
      • #!/usr/bin/env python

        # -*- coding:utf-8 -*-

        # Author:Lijunjie

         

        """

        Filearraysortedlistiterator.py

        """

         

         

        class ArraySortedListIterator(object):

            """Represents the list iterator for a sorted array list."""

         

            def __init__(self, backingStore):

                """Set the initial state of the list iterator."""

                self._backingStore = backingStore

                self._modCount = backingStore.getModCount()

                self._cursor = 0

                self._lastItemPos = -1

                self.first()

         

            def first(self):

                """Resets the cursor to the beginning of the backing store."""

                self._cursor = 0

                self._lastItemPos = -1

         

            def last(self):

                """Moves the cursor to the end of the backing store."""

                self._cursor = len(self._backingStore)

                self._lastItemPos = -1

         

            def hasNext(self):

                """Return True if the iterator has a next item or False otherwise."""

                return self._cursor < len(self._backingStore)

         

            def next(self):

                """Precondition: hasNext returns True.The list has not been modified except by this iterator‘s mutator.

                Returns the current item and advances the cursor to the next item."""

                if not self.hasNext():

                    raise ValueError("No next item in list iterator.")

                if self._modCount != self._backingStore.getModCount():

                    raise AttributeError("Illegal modification of backing store.")

                self._lastItemPos = self._cursor

                self._cursor += 1

                return self._backingStore[self._lastItemPos]

         

            def hasPrevious(self):

                """Return True if the iterator has a previous item or False otherwise"""

                return self._cursor > 0

         

            def previous(self):

                """Precondition: hasPrevious returns True. The list has not been modified except by this iterator‘s mutator.

                """

                if not self.hasPrevious():

                    raise ValueError("No previous item in list iterator.")

                if self._modCount != self._backingStore.getModCount():

                    raise AttributeError("Illegal modification of backing store.")

                self._cursor -= 1

                self._lastItemPos = self._cursor

                return self._backingStore[self._lastItemPos]

         

            def remove(self):

                """Precondition: the current position is defined. The list has not been modified except by this iterator‘s

                mutator."""

                if self._lastItemPos == -1:

                    raise AttributeError("The current position is undefined.")

                if self._modCount != self._backingStore.getModCount():

                    raise AttributeError("List has been modified illegally.")

                self._backingStore.pop(self._lastItemPos)

                # If the item removed was obtained via next, move cursor back

                if self._lastItemPos < self._cursor:

                    self._cursor -= 1

                self._modCount += 1

                self._lastItemPos = -1

     

    • ArrayListIteraotr
      • #!/usr/bin/env python

        # -*- coding:utf-8 -*-

        # Author:Lijunjie

         

        """

        Filearraylistiterator.py

        """

         

        from arraysortedlistiterator import ArraySortedListIterator

         

         

        class ArrayListIterator(ArraySortedListIterator):

            """Represents the list iterator for an array list."""

         

            def __init__(self, backingStore):

                """Set the initial state of the list iterator."""

                ArraySortedListIterator.__init__(self, backingStore)

         

            def replace(self, item):

                """Precondition: the current position is defined. The list has not been modified except by this iterator‘s

                mutator."""

                if self._lastItemPos == -1:

                    raise AttributeError("The current position is undefined.")

                if self._modCount != self._backingStore.getModCount():

                    raise AttributeError("Illegal modification of backing store.")

                self._backingStore[self._lastItemPos] = item

                self._lastItemPos = -1

         

            def insert(self, item):

                """Precondition: The list has not been modified except by this iterator‘s mutator."""

                if self._modCount != self._backingStore.getModCount():

                    raise AttributeError("Illegal modification of backing store.")

                if self._lastItemPos == -1:

                    # Cursor not defined, so add item to end of list.

                    self._backingStore.add(item)

                else:

                    self._backingStore.insert(self._lastItemPos, item)

                # If the item insert was obtained via next, move cursor forward

                if self._lastItemPos < self._cursor:

                    self._cursor += 1

                self._lastItemPos = -1

                self._modCount += 1 

     

     

以上是关于数据结构( Pyhon 语言描述 ) — —第9章:列表的主要内容,如果未能解决你的问题,请参考以下文章

数据结构( Pyhon 语言描述 ) — —第11章:集和字典

day01(pyhon基础)

Pyhon全栈之路----数据类型

内核级pyhon:编译python编译器和语法修改

数据结构c++语言描述——最大堆(MaxHeap)

appium自动化测试-一些pyhon操作