zoukankan      html  css  js  c++  java
  • 我也要学python内置数据结构(一)

      学完了C后,现在学python感觉好有效率的啊!PYTHON语言本身就内置了好多非常常用的数据结构,开发效率太高了,我在学python的时候一直在想,这个方法用C如何实现啊,然后自己再用C去实现,这样学感觉特有趣,也特有效率。都快忍不住要去悄悄PYTHON的源码啦!

    关于python的学习笔记我是这样安排的:

     内置数据结构(一 二)
     模块与函数
     字符串与正则表达式
     文件的处理
     面向对象编程
     程序调试、数据库编程、WxPython库

     WxPython库中的基本控件
     菜单、窗口与对话框
     WxPython库中的高级控件 
     WxPython学生信息管理系统的实现

    一共2个星期学完,呵呵,最后写个PYTHON wx版的学生信息管理系统,因为之前用C写过啦!很有信心哦!

    元组

      元组是一组不可修改的元素的集合

    元组代码

    解释输出为:


    元组的只读性:

     

    元素的遍历:

    列表

      列表也是一组元素的集合,它不同与于元组的是,列表中的元素是可以修改的。

    代码:

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

    #定义一个列表
    list = ["apple", "banana", "grape", "orange"]
    #直接输出列表
    print list
    #得到第3个元素
    print list[2]
    #append是加在列表的最后面
    list.append("watermelon")
    #在第二个元素后面插入
    list.insert(1, "grapefruit")
    #再次输出
    print list
    #删除1个元素
    list.remove("grape")
    print list
    #如果这里去掉注释会出错,因为字符串a并不存在列表里,删除一个不存在的元素时会提示错误!
    #
    list.remove("a")
    #
    弹出列表最后1个元素
    print list.pop()
    print list

    list
    = ["apple", "banana", "grape", "orange"]
    #对列表进行分片操作,和对元素分片类试
    print list[-2]
    print list[1:3]
    print list[-3:-1]
    #定义一个二元列表
    list = [["apple", "banana"], ["grape", "orange"], ["watermelon"], ["grapefruit"]]
    #遍历
    for i in range(len(list)):
    print "list[%d] :" % i, "" ,
    for j in range(len(list[i])):
    print list[i][j], "" ,
    print

    解释输出为:

    View Code
    ---------- python ----------
    [
    'apple', 'banana', 'grape', 'orange']
    grape
    [
    'apple', 'grapefruit', 'banana', 'grape', 'orange', 'watermelon']
    [
    'apple', 'grapefruit', 'banana', 'orange', 'watermelon']
    watermelon
    [
    'apple', 'grapefruit', 'banana', 'orange']
    grape
    [
    'banana', 'grape']
    [
    'banana', 'grape']
    list[0] : apple banana
    list[
    1] : grape orange
    list[
    2] : watermelon
    list[
    3] : grapefruit

    输出完成 (耗时 0 秒)
    - 正常终止

    FOR IN 循环放入列表里:

    这是列表不同于元组的特殊用法,呵呵!

    列表的查找,排序与反转:

    代码与注释:

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

    list
    = ["apple", "grape", "grape", "orange"]
    list.remove(
    "grape")
    print list

    list
    = ["apple", "banana", "grape", "orange"]
    #通过元素取索引
    print list.index("grape")
    print list.index("orange")
    #判断某元素是否在列表里面
    print "orange" in list

    list1
    = ["apple", "banana"]
    list2
    = ["grape", "orange"]
    #扩展列表
    list1.extend(list2)
    print list1
    list3
    = ["watermelon"]
    #列表的加法操作
    list1 = list1 + list3
    print list1
    #扩展列表
    list1 += ["grapefruit"]
    print list1
    #进行复制操作
    list1 = ["apple", "banana"] * 2
    print list1

    #使用列表的sort方法排序
    list = ["banana", "apple", "orange", "grape"]
    #首字母升序进行排序
    list.sort()
    print "sorted list:", list
    #反转操作
    list.reverse()
    print "Reversed list:", list

    #使用函数sorted排序,返回一个新的列表
    list = ["banana", "apple", "orange", "grape", "apple"]
    #set重复数据进行过虑并排序
    for li in sorted(set(list)):
    print li, "" ,

    解释输出:

    ---------- python ----------
    [
    'apple', 'grape', 'orange']
    2
    3
    True
    [
    'apple', 'banana', 'grape', 'orange']
    [
    'apple', 'banana', 'grape', 'orange', 'watermelon']
    [
    'apple', 'banana', 'grape', 'orange', 'watermelon', 'grapefruit']
    [
    'apple', 'banana', 'apple', 'banana']
    sorted list: [
    'apple', 'banana', 'grape', 'orange']
    Reversed list: [
    'orange', 'grape', 'banana', 'apple']
    apple banana grape orange

    输出完成 (耗时 0 秒)
    - 正常终止

    堆栈队列的实现

      数据结构中堆栈和队列是一些很常见的线性结构哦!我以前学过C的实现它,现在来看看PYTHON的实现呀!

    代码:

      疯狂的写代码,疯狂的热爱吧!呵呵!

    The Connells 传奇单曲7475

  • 相关阅读:
    开发技术--Numpy模块
    开发技术-IPython介绍
    开发--Deepin系统安装
    开发--CentOS-7安装及配置
    开发技术--设计模式
    English--音标重难点
    English--音标拼读
    English--辅音
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/dodolook/p/2112530.html
Copyright © 2011-2022 走看看