GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python 练习实例39 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 基础教程
Python 基础教程 Python 简介 Python 环境搭建 Python 中文编码 Python VS Code Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循环语句 Python While 循环语句 Python for 循环语句 Python 循环嵌套 Python break 语句 Python continue 语句 Python pass 语句 Python Number(数字) Python 字符串 Python 列表(List) Python 元组 Python 字典(Dictionary) Python 日期和时间 Python 函数 Python 模块 Python 文件I/O Python File 方法 Python 异常处理 Python OS 文件/目录方法 Python 内置函数

Python 高级教程

Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3​​.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验
Python 练习实例38
Python 练习实例40

Python 练习实例39

Python 100例 Python 100例

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。

程序源代码:

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': # 方法一 : 0 作为加入数字的占位符 a = [1,4,6,9,13,16,19,28,40,100,0] print ('原始列表:') for i in range(len(a)): print (a[i]) number = int(input("\n插入一个数字:\n")) end = a[9] if number > end: a[10] = number else: for i in range(10): if a[i] > number: temp1 = a[i] a[i] = number for j in range(i + 1,11): temp2 = a[j] a[j] = temp1 temp1 = temp2 break print ('排序后列表:') for i in range(11): print (a[i])

以上实例输出结果为:

原始列表:
1
4
6
9
13
16
19
28
40
100
0

插入一个数字:
7
排序后列表:
1
4
6
7
9
13
16
19
28
40
100

Python 100例 Python 100例

AI 思考中...
Python 练习实例38
Python 练习实例40

11 篇笔记 写笔记

  1. #0

       啊香魂

      wei***[email protected]

    12

    参考解法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    a = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
    b = 18
    a.append(b)
    c = a[:]
    l = len(c)
    
    # 从后面开始,如果比倒数第二个数大,那就将新加入的数填在倒数第一的位置,否则倒数第二的数位置后移
    for i in range(l,0,-1):
       if (b>c[i-2]):
          c[i-1] =b
          break
       else:
          c[i-1] = c[i-2]
    print c
    
    啊香魂

       啊香魂

      wei***[email protected]

    10年前 (2017-04-21)
  2. #0

       鱼尾巴的鱼

      824***[email protected]

      参考地址

    5

    参考方案:

    #!/usr/bin/python
    #coding:utf-8
    
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    #题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
    list = [1,2,3,4,5,6,8,9,10,11,12,13]
    print list
    #我将通过插入数字7来加入按照从小到大排列的列表中
    n = int(raw_input(""))
    #通过for循环来讲数字在列表中定位,然后将数字添加进去就可以了。
    for i in range(0,13):
        if list[i] < n < list[i+1]:
            list.insert(i+1,n)
    print u"插入数字后的列表为:\n",list
    
    鱼尾巴的鱼

       鱼尾巴的鱼

      824***[email protected]

      参考地址

    10年前 (2017-04-25)
  3. #0

       animo

      z@g***cc

    37

    参考方法:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    __author__ = 'Lei Zhong'
    x=[1,3,5,6,88,99]
    y=int(raw_input("输入数字: "))
    x.append(y)
    x.sort()
    print(x)
    animo

       animo

      z@g***cc

    10年前 (2017-04-27)
  4. #0

       等一个人

      252***[email protected]

    2

    参考解法:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    L = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
    print('原始列表为: {}'.format(L))
    num = int(input('输入一个数:'))
    L.append(num)
    for i in range(1, len(L)+1):
        if L[-i] < L[-(i+1)]:
            L[-i], L[-(i+1)] = L[-(i+1)], L[-i]
        else:
            break
    print('新的列表为: {}'.format(L))
    
    等一个人

       等一个人

      252***[email protected]

    10年前 (2017-05-03)
  5. #0

       JohnLee

      372***[email protected]

    0

    Python3 参考方法:

    # !/usr/bin/env python3
    
    a = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]
    b = 18
    #对半查找,提高效率
    f0, f1 = 0, len(a) - 1
    while f1 - f0 > 1:
        mid = f0 + int((f1 - f0) / 2)
        if a[mid] > b:
            f1 = mid
        else:
            f0 = mid
    a.insert(f0 + 1, b)
    print(a)
    
    JohnLee

       JohnLee

      372***[email protected]

    10年前 (2017-05-17)
  6. #0

       dayu

      923***[email protected]

    0

    参考方法:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    l = [2,5,9,12,89]
    num = input("enter the num:")
    if num > l[len(l)-1]:
        l.append(num)
    elif num < l[0]:
        l.insert(0,num)
    else:
        for i in range(len(l)-1):
            if l[i] < num and  num < l[i+1]:
                l.insert(i+1,num)
                break
    print l
    dayu

       dayu

      923***[email protected]

    10年前 (2017-05-18)
  7. #0

       朦胧

      253***[email protected]

    6

    Python3 测试参考:

    # coding:utf-8
    List1=[2,5,4,8,3,1,9]
    List1=sorted(List1)
    print("打印原来的数组:",List1)
    print()
    
    num=int(input("请输入一个数:"))
    List1.append(num)
    print("打印新的数组:",sorted(List1))
    
    朦胧

       朦胧

      253***[email protected]

    10年前 (2017-06-13)
  8. #0

       Think_dfrent

      iwa***[email protected]

    1

    Python3实例,新建一个列表存储插入后的数组,先将原数组元素一一与该数比较,把较小值插入新列表,若该数已被插入,则将原数组剩余的数插入新列表,若原数组都被遍历完仍未插入,则将该数插到新列表最后

    #!/usr/bin/python3
    carray=[1,4,6,9,13,16,19,28,40,100,110]
    n=int(input("please enter a number:\n"))
    
    newarray=[]
    flag,a=0,0              #flag作为n有无被插入的标志位
    while a<len(array):
        if array[a]<n or flag==1:
            newarray.append(array[a])
        else:
            newarray.append(n)
            a=a-1        
            flag=1    
        a=a+1
    
    if flag==0:
        newarray.append(n)
    
    print(newarray)
    Think_dfrent

       Think_dfrent

      iwa***[email protected]

    10年前 (2017-06-26)
  9. #0

       colinshi

      col***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    def px(n):
        l=[1,4,6,9,13,16,19,28,40,100]
        d=[]
        if n>l[-1]:
            #首先考虑是不是这个数字大于真的字符串最大的数字。如果大约直接插入到最后,结束。
            l.append(n)
            print (l)
        else:
            #然后对这个数字进行和字符串起始处比较,小于数字的字符串直接插入新的字符串中。
            while len(l) >0 :
                l1=l.pop(0)
                if l1 < n:
                    d.append(l1)
                else:
                    #当比较结果大于插入数字时,先插入该数字,然后将比较字符插入到后面,加上剩余字符串,结束。
                    d.append(n)
                    d.append(l1)
                    d=d+l
                    break
            print(d)
    
    if __name__=='__main__':
        px(111)
        px(0)
        px (10)
    
    colinshi

       colinshi

      col***[email protected]

    10年前 (2017-07-07)
  10. #0

       大大大大大大大熊

      382***[email protected]

    1

    排序是从小到大:

    a=[1,2,3,4,5,6,9]
    b=7
    for i in range(len(a)-1):
        if b >=a[i] and b<=a[i+1]:
            a.insert(i+1, b)
            break
        elif i==(len(a)-2):
            a.append(b)
            break
    print(a)
    
    大大大大大大大熊

       大大大大大大大熊

      382***[email protected]

    9年前 (2017-09-20)
  11. #0

       py托马斯

      152***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    arr = list(map(int, raw_input("输入一串数字:").split()))
    arr.sort()  #排序
    print arr
    arr1 = []
    n = int(raw_input('请输入一个数字:'))
    for i in range(len(arr)):
        #将从插入n的位置分成两个列表,将n插入第一列表尾再合并第二个列表为一个列表
        if arr[i] < n and (arr[i+1] == n or arr[i+1] > n):
           arr1.extend(arr[0:i+1])
           arr1.append(n)      
           arr1.extend(arr[i+1:len(arr)]) 
           break
    print arr1

    测试结果:

    输入一串数字:1 23 3 5 22 12
    [1, 3, 5, 12, 22, 23]
    请输入一个数字:9
    [1, 3, 5, 9, 12, 22, 23]
    py托马斯

       py托马斯

      152***[email protected]

    9年前 (2017-10-24)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.