当前位置: 首页>>代码示例>>Python>>正文


Python Stack.append方法代码示例

本文整理汇总了Python中stack.Stack.append方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.append方法的具体用法?Python Stack.append怎么用?Python Stack.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stack.Stack的用法示例。


在下文中一共展示了Stack.append方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: dfs_iterative_2

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def dfs_iterative_2(root, result):
    '''depth first search iterative implementation
        children are traversed in a different order to previous algorithms (but its more efficient)''' 
    todo = Stack()
    todo.append(root)
    while len(todo):
        node = todo.pop()
        result.append(node.visit())
        for node in node.children:
            todo.append(node)
开发者ID:cbernet,项目名称:python-scripts,代码行数:12,代码来源:tree.py

示例2: dfs_iterative

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def dfs_iterative(root, result):
    '''depth first search iterative implementation
        in same order as for recursion'''    
    todo = Stack()
    todo.append(root)
    while len(todo):
        node = todo.pop()
        result.append(node.visit())
        for node in reversed(node.children): #reversal makes it match dfs_recursive
            todo.append(node)
开发者ID:cbernet,项目名称:python-scripts,代码行数:12,代码来源:tree.py

示例3: inorder_iterative_visit1

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def inorder_iterative_visit1(root, result):
    '''
    in-order, iterative implementation.
    
    requires a visited flag to be added to each node.
    not very elegant, but obvious solution to cycling problem.
    In python I don't think this solution is better than the recursive one,
    because we still have a stack -> so mem usage is the same.
    In C++ or java, the stack should be declared on the heap...
    otherwise I don't see what is the gain. 
    '''
    todo = Stack()
    todo.append( root )
    while len(todo):
        node = todo.peek()
        if node:
            if not node.visited:
                node.visited = True
                todo.append( node.left )
            else:
                result.append( node.visit() ) # changed to node.visit to make more general
                todo.pop()
                todo.append( node.right )
        else:
            print 'none'
            todo.pop()
开发者ID:cbernet,项目名称:python-scripts,代码行数:28,代码来源:binary_tree.py

示例4: inorder_iterative_visit2

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def inorder_iterative_visit2(root, result):
    '''cleaner and more understandable than visit1, but there is still 
    a visited flag'''
    todo = Stack()
    todo.append( root )
    while len(todo):
        node = todo.pop()
        if node:
            if not node.visited:
                node.visited = True
                todo.append( node ) # re-adding the node for second visit
                todo.append( node.left )
            else:
                result.append( node.visit() ) # changed to node.visit to make more general
                todo.append( node.right )
开发者ID:cbernet,项目名称:python-scripts,代码行数:17,代码来源:binary_tree.py

示例5: inorder_iterative

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def inorder_iterative(root, result):
    '''Finally, without the visited flag... 
    took me some time to find this one...'''
    todo = Stack()
    todo.append( root )
    last = None
    while len(todo):
        node = todo.pop()
        if node:
            if not last or (last.left is node or last.right is node):
                todo.append( node )
                todo.append( node.left )
            else:
                result.append( node.visit() ) # changed to node.visit to make more general
                todo.append( node.right )
            last = node 
开发者ID:cbernet,项目名称:python-scripts,代码行数:18,代码来源:binary_tree.py

示例6: preorder_iterative

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def preorder_iterative(root, result):
    '''pre-order, iterative implementation'''
    todo = Stack()
    todo.append(root)
    while len(todo):
        node = todo.pop()
        if node.right:
            todo.append(node.right)
        if node.left:
            todo.append(node.left)
        result.append( node.visit() )
开发者ID:cbernet,项目名称:python-scripts,代码行数:13,代码来源:binary_tree.py

示例7: postorder_iterative_1

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def postorder_iterative_1(root, result):
    '''We effectively find the nodes from last to first
    so when we find each node we append to the start of the result not the back    
    This version uses visited'''
    todo = Stack()
    todo.append( root )
    while len(todo):
        node = todo.peek()
        if node:
            if not node.visited:
                node.visited = True
                result.insert(0,node.visit()) #add to back
                todo.append( node.right )
            else:
                todo.pop()
                todo.append( node.left )
        else:
            todo.pop()
开发者ID:cbernet,项目名称:python-scripts,代码行数:20,代码来源:binary_tree.py

示例8: postorder_iterative

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def postorder_iterative(root, result):
    '''version without visited
    we find the nodes from last to first
    so each node is inserted at the start of the result not the back
    an alternative might be to reverse the result at the end'''  
    todo = Stack()
    todo.append( root )
    last = None
    while len(todo):
        node = todo.peek()
        if node:
            if not last or last.right is node or last.left is node:
                result.insert(0,node.visit())
                todo.append( node.right )
            else:
                todo.pop()
                todo.append( node.left )
            last=node
        else:
            todo.pop()
开发者ID:cbernet,项目名称:python-scripts,代码行数:22,代码来源:binary_tree.py

示例9: LEXdfs

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
def LEXdfs(graph, start):
    """
    Does DFS search of graph, beginning at start.
    Implemented from Algorithm 1 in
    "Finding compact communities in large graphs"
    by Creusefond, Largillier and Peyronnet.
    http://dx.doi.org/10.1145/2808797.2808868 
    """

    #
    # Create and initialize VISITED and LEX for all nodes
    #
    attrs = { VISITED: {},
              LEX: {}}
    
    node = graph.BegNI()
    while node < graph.EndNI():
        attrs[VISITED][node.GetId()] = 0
        attrs[LEX][node.GetId()] = "0"
        node.Next()
    
    # initialize DFS variables
    stack = Stack()
    stack.append( start.GetId() )
    i = 1

    # do the search
    while len(stack) > 0:

        # print "stack:"
        # print node_list_to_str(graph, stack, attrs)
        # print
        # print
        
        # process top node
        # print
        # stack.print_all()
        # print
        node_id = stack.pop()
        node = graph.GetNI(node_id)
        attrs[VISITED][node_id] = i
        array = []
        
        # find unvisited neighbors of node
        for in_id in range(node.GetOutDeg()):
            out_id = node.GetOutNId(in_id)
            out_node = graph.GetNI(out_id)
            if attrs[VISITED][out_id] == 0:
                # will raise exception if out_node not there
                try:
                    # print "Trying to remove", node_to_str(graph, out_id, attrs)
                    stack.remove(out_id)
                    # print "Removed", node_to_str(graph, out_id, attrs)
                except ValueError as e:
                    # expected to occur
                    pass

                attrs[LEX][out_id] = str(i) + attrs[LEX][out_id]
                array.append(out_id)

            # end of unvisited neighbor
        # end of neighbors

        # print "Not sure if this is correct.  Needs to randomize order for ties"
        # print "Before"
        # print node_list_to_str(graph, array, attrs)
        array.sort(key = lambda n_id: attrs[LEX][n_id])
        randomize_equal_neighbors(graph, array, attrs)
        # print "After"
        # print node_list_to_str(graph, array, attrs)
        # print
        # print
        stack.extend(array)
        i = i + 1
        # print "stack:"
        # print node_list_to_str(graph, stack, attrs)
        # print
        # print

    # end of stack processing
    
    return attrs
开发者ID:fractal13,项目名称:information-spread-project,代码行数:84,代码来源:LEXdfs.py

示例10: Gaot

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import append [as 别名]
class Gaot(object):
    def __init__(self):
        self.stack = Stack([])
        self.instructions = None
        self.ipointer = 0
        self.move = 1
        self.BAA = re.compile(r"ba{2,}")
        self.BLEET = re.compile(r"ble{2,}t")

        self.commands = [
            self._add,  # 2
            self._subtract,  # 3
            self._continue,  # 4
            self._exit,  # 5
            self._reverse,  # 6
            self._skiptrue,  # 7
            self._skipfalse,  # 8
            self._printnum,  # 9
            self._printchar,  # 10
            self._getnum,  # 11
            self._getchar,  # 12
            self._dupe,  # 13
            self._swap,  # 14
            self._reverse_stack,  # 15
            self._rotate_stack,
        ]  # 16
        self.ccount = 0

    def run(self, program):
        self.instructions = program.split()
        while self.ipointer < len(self.instructions):
            c = self.instructions[self.ipointer]

            if self.BAA.search(c):  # is a "baa" command
                self.stack.append(len(c) - 2)

            elif self.BLEET.search(c):  # is a "bleet" command
                n = len(c) - 3
                self.commands[n - 2]()
            self.ipointer += self.move

    def get_code(self):
        if "--file" in sys.argv:
            code = open(sys.argv[sys.argv.index("--file") + 1]).read()

            input_index = sys.argv.index("--file")

            sys.argv.pop(sys.argv.index("--file") + 1)
            sys.argv.pop(sys.argv.index("--file"))

        elif len(sys.argv) >= 2:
            code = sys.argv[1]
            input_index = 2
        else:
            code = raw_input("Code: ")

        try:
            self.input = sys.argv[input_index:]
        except:
            self.input = []
        return code

        # --------------------------------------

    def _add(self):
        self.stack.push(self.stack.pop() + self.stack.pop())

    def _subtract(self):
        self.stack.push(self.stack.pop() - self.stack.pop())

    def _continue(self):
        self.ipointer += self.move

    def _exit(self):
        exit()

    def _reverse(self):
        self.move *= -1

    def __skip(self, state):
        self.ipointer += self.move * (self.stack.pop() == state)

    def _skiptrue(self):
        self.__skip(True)

    def _skipfalse(self):
        self.__skip(False)

    def _printnum(self):
        sys.stdout.write(unicode(self.stack.pop()))
        sys.stdout.flush()

    def _printchar(self):
        sys.stdout.write(unicode(chr(self.stack.pop())))
        sys.stdout.flush()

    def _getnum(self):
        self.stack.push(float(raw_input()))

    def _getchar(self):
#.........这里部分代码省略.........
开发者ID:RikerW,项目名称:gaot,代码行数:103,代码来源:main.py


注:本文中的stack.Stack.append方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。