python系列21:python版本linked list

Posted IE06

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python系列21:python版本linked list相关的知识,希望对你有一定的参考价值。

pip install pyllist进行安装,据称性能强于collections.deque和默认的Python lists,dllist是double linked list,sllist是single linked list。
下面是常用方法:

>>> from pyllist import dllist, dllistnode
>>> lst = dllist([1, 2, 3])       # create and initialize a list
>>> print lst                     # display elements in the list
dllist([1, 2, 3])

>>> print len(lst)                # display length of the list
3
>>> print lst.size
3

>>> print lst.nodeat(0)           # access nodes by index
dllistnode(1)

>>> print lst[0]                  # access elements by index
1

>>> node = lst.first              # get the first node (same as lst[0])
>>> print node
dllistnode(1)

>>> print node.value              # get value of node
1
>>> print node()                  # get value of node
1
>>> print node.prev               # get the previous node (nonexistent)
None
>>> print node.next               # get the next node
dllistnode(2)
>>> print node.next.value         # get value of the next node
2

>>> for value in lst:             # iterate over list elements
...     print value * 2,
2 4 6

>>> for node in lst.iternodes():  # iterate over list nodes
...     print node,
dllistnode(1) dllistnode(2) dllistnode(3)

>>> for node in lst.nodeat(1).iternext():        # iterate starting node with index 1
...     print node,
dllistnode(2) dllistnode(3)

>>> for node in lst.nodeat(1).iterprev():        # iterate back starting node with index 2
...     print node,
dllistnode(2) dllistnode(1)

>>> lst.appendright(4)            # append value to the right side of the list
<dllistnode(4)>
>>> print lst
dllist([1, 2, 3, 4])
>>> new_node = dllistnode(5)
>>> lst.appendright(new_node)     # append value from a node
<dllistnode(5)>
>>> print lst
dllist([1, 2, 3, 4, 5])
>>> lst.appendleft(0)             # append value to the left side of the list
<dllistnode(0)>
>>> print lst
dllist([0, 1, 2, 3, 4, 5])

>>> node = lst.nodeat(2)
>>> lst.insert(1.5, node)         # insert 1.5 before node
<dllistnode(1.5)>
>>> print lst
dllist([0, 1, 1.5, 2, 3, 4, 5])
>>> lst.insert(6)                 # append value to the right side of the list
<dllistnode(6)>
>>> print lst
dllist([0, 1, 1.5, 2, 3, 4, 5, 6])

>>> lst.popleft()                 # remove leftmost node from the list
0
>>> print lst
dllist([1, 1.5, 2, 3, 4, 5, 6])
>>> lst.popright()                # remove rightmost node from the list
6
>>> print lst
dllist([1, 1.5, 2, 3, 4, 5])
>>> node = lst.nodeat(1)
>>> lst.remove(node)              # remove 2nd node from the list
1.5
>>> print lst
dllist([1, 2, 3, 4, 5])
>>> cmp(dllist(), dllist([]))     # list comparison (lexicographical order)
0
>>> cmp(dllist([1, 2, 3]), dllist([1, 3, 3]))
-1
>>> cmp(dllist([1, 2]), dllist([1, 2, 3]))
-1
>>> cmp(dllist([1, 2, 3]), dllist())
1

>>> lst1 = dllist([1, 2, 3, 4])   # extending lists
>>> lst2 = dllist([5, 6, 7, 8])
>>> ext_lst = lst1 + lst2
>>> print ext_lst
dllist([1, 2, 3, 4, 5, 6, 7, 8])

>>> lst = dllist([1, 2, 3, 4])
>>> ext_lst = lst * 2
>>> print ext_lst
dllist([1, 2, 3, 4, 1, 2, 3, 4])

以上是关于python系列21:python版本linked list的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu python多个版本管理

1.跟老韩学Python之工具那些事儿

1.跟老韩学Python之工具那些事儿

[Python系列-21]:Python之人工智能 - 基本工具 - 5- 绘制二元函数的三维曲线或散点图

CentOS 7.x下升级Python版本到3.x系列(新老版本共存)

python - 使用 python 和 pymssql 获取一系列行号之间的行