当前位置:网站首页>实例039:有序列表插入元素
实例039:有序列表插入元素
2022-08-04 01:35:00 【懒笑翻】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
代码:
方法一:
lis = [1, 10, 100, 1000, 10000, 100000]
n = int(input('insert a number: '))
lis.append(n)
for i in range(len(lis) - 1):
if lis[i] >= n:
for j in range(i, len(lis)):
lis[j], lis[-1] = lis[-1], lis[j]
break
print(lis)
方法二:
lis = [1, 10, 100, 1000, 10000, 100000]
n = int(input('insert a number: '))
lis.append(n)
print(sorted(lis))结果:

"""
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群,扫码下方官方推荐的群即可进入哦
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!↓↓↓↓↓↓
"""边栏推荐
- The idea of the diagram
- Flask Framework Beginner-05-Command Management Manager and Database Use
- GNSS[0]- Topic
- C 学生管理系统_分析
- typescript52 - simplify generic function calls
- 有没有jdbc 链接优炫数据库文档及示例?
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
- 114. How to find the cause of Fiori Launchpad routing error by single-step debugging
- 数组_滑动窗口 | leecode刷题笔记
猜你喜欢
![[store mall project 01] environment preparation and testing](/img/78/415b18a26fdc9e6f59b59ba0a00c4f.png)
[store mall project 01] environment preparation and testing

redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)

flask框架初学-06-对数据库的增删改查

esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)

typescript53-泛型约束

持续投入商品研发,叮咚买菜赢在了供应链投入上

HBuilderX的下载安装和创建/运行项目

《Greenplum构建实时数据仓库实践》简介

js中常用的几种遍历处理数据的方法梳理

ASP.NET 获取数据库的数据并写入到excel表格中
随机推荐
- heavy OpenCV 】 【 mapping
【日志框架】
typescript57 - Array generic interface
持续投入商品研发,叮咚买菜赢在了供应链投入上
Is there any jdbc link to Youxuan database documentation and examples?
typescript53 - generic constraints
typescript53-泛型约束
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
敏捷交付的工程效能治理
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
GNSS文章汇总
Demand analysis of MES management system in electronic assembly industry
esp32发布机器人电池电压到ros2(micro-ros+CoCube)
Slipper —— 虚点,最短路
2022 中国算力大会发布“创新先锋”优秀成果
螺旋矩阵_数组 | leecode刷题笔记
工程制图名词解释-重点知识
initramfs详解----添加硬盘驱动并访问磁盘
GNSS【0】- 专题