zoukankan      html  css  js  c++  java
  • python入门(七)

    python数据结构

    一、数据结构概述

      数据组织在一起的结构叫做数据结构。

      python中的数据结构:列表,元组,字典,队列,栈,树等等。

      python内置数据结构:列表、元组等。
      python扩展数据结构:栈,队列等。
      数据结构式静态的,算法是动态的。

      数据结构示例:

     1 #coding=utf-8
     2 '''
     3 Created on 2016��4��20��
     4 
     5 @author: Administrator
     6 '''
     7 #python内置数据结构,元组、列表、字典
     8 #将三个物品"apple"、"orange"、"pear"组织起来
     9 #存储方式一,可以取出来(列表)
    10 a=["apple","orange","pear"]
    11 print a
    12 #存储方式二,不可以取出来,不可以改变(元组)
    13 b=("apple","orange","pear")
    14 print b
    15 #存储方式三,不仅按顺序存储,存储空间还有名字
    16 c={"sam":"apple","jac":"orage","mating":"pear"}
    17 print c

    二、Python 常见数据结构-栈

      栈是一端开口,一端开放的容器。

      代码示例: 

     1 #coding=utf-8
     2 #
     3 class Stack:
     4     #初始化栈
     5     def __init__(self,size):#栈的主体,与栈的容量
     6         self.stack=[]#声明栈的属性,此时栈的属性为列表
     7         self.size=size#传递栈的容量
     8         self.top=-1#初始化栈顶的位置,有数据为0,没有数据为-1
     9     
    10     #入栈,先判断栈是否已经满了   
    11     def push(self,content):
    12         if self.Full():
    13             print "Stack if full!"
    14         else:
    15             self.stack.append(content)#数据加入栈中,append()增加内容
    16             self.top=self.top+1#修改栈顶指针
    17     
    18     #出栈
    19     def out(self):
    20         if self.Empty():#判断栈是否为空
    21             print "Stack is Empty!"
    22         else:
    23             self.top=self.top-1#栈顶指针减一
    24     
    25     def Full(self):
    26         if self.top==self.size:#栈顶指针定于栈的大小
    27             #print"Stack is Empty!"
    28             return True
    29         else:
    30             return False
    31         
    32     def Empty(self):
    33         if self.top==-1:
    34             return True
    35         else:
    36             return False
    37     def printStack(self):
    38         i = self.top
    39         print "amount of elements:{0}".format(self.top + 1)
    40         while i >= 0:
    41             print self.stack[i]
    42             print "--"
    43             i -= 1
    44 '''
    45 st=Stack(7)
    46 Stack.Empty(st)   
    47 Stack.push(st,"hello")
    48 Stack.Empty(st)
    49 Stack.Full(st)
    50 Stack.push(st,"7")
    51 Stack.printStack(st)
    52 Stack.out(st)
    53 Stack.out(st)
    54 Stack.Empty(st)
    55 '''
    56 st=Stack(3)
    57 st.printStack()
    58 st.push(1)
    59 st.printStack()
    60 st.push(2)
    61 st.printStack()
    62 st.push(3)
    63 st.printStack()
    64 st.push(4)
    65 st.printStack()
    66 st.Full()
    67 #st.push(5)
    68 #st.printStack()       
    69             
    70             

      运行结果如图所示:

      栈的代码运行结果            栈的代码运行结果

      运行结果解释:

      栈的空间是4,因为第一个数据是0,空间是3=1=4

      push()5个才出现是因为4的时候没有再次进行判断。
      Full()加上print()再执行就有显示栈满。
      所以栈的空间为4尔不是5.

    三、Python 常见数据结构-队列

      队列在队尾插入,队首删除。

      代码示例:

     1 #coding=utf-8
     2 #队列的实现
     3 class Queue1:
     4     
     5     #初始化队列
     6     def __init__(self,size):
     7         self.queue=[]
     8         self.size=size
     9         self.head=-1
    10         self.tail=-1
    11 
    12     def enQueue(self,contest):
    13         if self.Full():
    14             print "Queue if full!"
    15         else:
    16             self.queue.append(contest)
    17             self.tail=self.tail+1
    18     def outQueue(self):
    19         if self.Empty():
    20             print "Queue is Empty"
    21         else:
    22             self.head=self.head+1
    23     
    24     def Empty(self):
    25         if self.tail==self.head:
    26             return True
    27         else:
    28             return False
    29     
    30     def Full(self):
    31         if (self.tail-self.head)==self.size:
    32             print "Queue if full"
    33             return True
    34         else:
    35             return False
    36 
    37 qz=Queue1(3)
    38 qz.enQueue(1)
    39 qz.enQueue(2)
    40 qz.enQueue(3)
    41 qz.Empty()
    42 qz.enQueue(4)

      运行结果如下图:

           队列代码运行结果

    2016-04-21    02:10:26

    本性的苏醒,往往在遭遇真实之后。
  • 相关阅读:
    leetcode778
    2020年的一些总结
    go笔记 NSQ (6) ( nsqd如何创建消费者以及消费消息)
    go笔记 NSQ (5) ( nsqd如何监听生产者的消息,select关键字使用)
    go笔记 NSQ (4) ( nsqd启动监听来了解go如何编写tcp与http服务端,以及sync.WaitGroup线程同步工具使用 )
    go笔记 NSQ (3) ( 从启动nsqd了解flag包使用,解析配置文件以及json有关,反射使用 )
    Sitecore去除地址中带语言,例:localhost:8080/zh-cn/index
    js给URL追加参数
    SQL Server服务启动时错误:1069(由于登陆失败而无法启动服务)
    C# windows服务定时处理/例每天凌晨1点处理数据
  • 原文地址:https://www.cnblogs.com/chance88/p/5415304.html
Copyright © 2011-2022 走看看