* count.py

list.append(xxx) 再 list.reverse 等效 list.insert(0, xxx)

#!/usr/bin/python
# -*- coding: utf-8 -*-

count = 10 ** 5
nums = []

for i in range(count):
    #    nums.append(i)
    nums.insert(0, i)
    
# nums.reverse()
print(nums)

* Nodes.py

python 简单的链表创建

#!/usr/bin/python
# -*- coding: utf-8 -*-

class Node:
    def __init__(self, value, next = None):
        self.value = value
        self.next = next
        
    def toString(self):
        p = self
        s = p.value
        p = p.next
        while (p != None):
           s += "," + p.value
           p = p.next
        return s
           
        
L = Node("a", Node("b", Node("c", Node("d"))))
# print(L.next.next.value)
print( L.toString() )

* graph.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

a, b, c, d, e, f, g, h = range(8)
N = [
    {b, c, d, e, f}, # a
    {c, e}, # b
    {d}, # c
    {e}, # d
    {f}, # e
    {c, g, h}, # f
    {f, h}, # g
    {f, g} # h
]

# Neighborhoold membership
print( b in N[a] )  # True
# Degree
print( len(N[f]) ) # 3

python 学习#1_初值

* matrix.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

a, b, c, d, e, f, g, h = range(8)

# Adjacency matrix
N = [[0,1,1,1,1,1,0,0], # a
     [0,0,1,0,1,0,0,0], # b
     [0,0,0,1,0,0,0,0], # c
     [0,0,0,0,1,0,0,0], # d
     [0,0,0,0,0,1,0,0], # e
     [0,0,1,0,0,0,1,1], # f
     [0,0,0,0,0,1,0,1], # g
     [0,0,0,0,0,1,1,0]] # h

# Neighborhoold membership
print( N[a][b] )  # 1
# Degree
print( sum(N[f]) )

* inf.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-

#  A Weight Matrix with Infinite Weight for Missing Edges
a, b, c, d, e, f, g, h = range(8)
_ = float('inf')

# a b c d e f g h
W = [[0,2,1,3,9,4,_,_], # a
     [_,0,4,_,3,_,_,_], # b
     [_,_,0,8,_,_,_,_], # c
     [_,_,_,0,7,_,_,_], # d
     [_,_,_,_,0,5,_,_], # e
     [_,_,2,_,_,0,2,2], # f
     [_,_,_,_,_,1,0,6], # g
     [_,_,_,_,_,9,8,0]] # h

# Neighborhoold membership
print( W[a][b] < _ )  # True
# Neighborhoold membership
print( W[c][e] < _ ) # False

# Degree
s = sum( 1 for w in W[a] if w < _ ) - 1
print( s ) # 5

Note that 1 is subtracted from the degree sum, because we don’t want to count the diagonal. 

 

创建一个矩阵10*10,每个元素赋初值为0

sudo pip3 install numpy

* zeros.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-

N = [ [0] * 10 for i in range(10) ]
print(N)

print("Using numpy...\n")

import numpy as np
N = np.zeros([10, 10])
print(N)

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Using numpy...

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

最新版的numpy代码库: svn co http://svn.scipy.org/svn/numpy/trunk numpy

* simpleTree.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

T = [ ["a","b"], ["c"], ["d", ["e", "f"]] ]
print(T[0][1])
print(T[2][1][0])

 

python 学习#1_svn_02

A sample tree with a highlighted path from the root to a leaf

* BinaryTree.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

class BinaryTree:
    def __init__(self, left, right):
        self.left = left
        self.right = right


t = BinaryTree(BinaryTree("a", "b"), BinaryTree("c", "d"))
print(t.right.left) # 'c'

python 学习#1_代码库_03

* Tree.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

class Tree:
    def __init__(self, kids, next = None):
        self.kids = self.val = kids
        self.next = next


t = Tree(Tree("a"), Tree("b", Tree("c", Tree("d"))))
print(t.kids.next.next.val)        # c

python 学习#1_链表_04

* Bunch.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-

# A flexible class that will allow you to specify arbitrary attributes in the constructor
# class Bunch extends dict
class Bunch(dict):
    def __init__(self, *args, **kwds):
        # super(Bunch, self).__init__(*args, **kwds)
        dict.__init__(self, *args, **kwds)
        self.__dict__ = self


x = Bunch(name="Jane Cobb", position="Public Relations")
print(x.name)   # Jane Cobb

T = Bunch
t = T(left=T(left="a", right="b"), right=T(left="c"))
print(t.left)  # {'right': 'b', 'left': 'a'}
print(t.left.right) # b
print(t['left']['right']) # b
print("left" in t.right) # True
print("right" in t.right) # False

python 学习#1_链表_05