当前位置:网站首页>实例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电子书!↓↓↓↓↓↓
"""边栏推荐
- 内网穿透-应用
- jmeter distributed stress test
- 通用的测试用例编写大全(登录测试/web测试等)
- 一个注解替换synchronized关键字:分布式场景下实现方法加锁
- Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
- typescript48 - type compatibility between functions
- 2022 中国算力大会发布“创新先锋”优秀成果
- 哎,又跟HR在小群吵了一架!
- 有没有jdbc 链接优炫数据库文档及示例?
- 观察者模式
猜你喜欢

typescript57 - Array generic interface

TensoFlow学习记录(二):基础操作

html select标签赋值数据库查询结果

idea中diagram使用

JS 保姆级贴心,从零教你手写实现一个防抖debounce方法

this巩固训练,从两道执行题加深理解闭包与箭头函数中的this

typescript51-泛型的基本使用

IDEA02:配置SQL Server2019数据库

Demand analysis of MES management system in electronic assembly industry

typescript50-交叉类型和接口之间的类型说明
随机推荐
Promise 解决阻塞式同步,将异步变为同步
《Greenplum构建实时数据仓库实践》简介
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
GraphQL背后处理及执行过程是什么
splice随机添加和删除的写法
有没有jdbc 链接优炫数据库文档及示例?
LDO investigation
How to find the cause of Fiori Launchpad routing errors by single-step debugging
typescript53-泛型约束
KunlunBase 1.0 is released!
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
JS 从零教你手写节流throttle
Array_Sliding window | leecode brushing notes
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
jmeter分布式压测
.NET Static Code Weaving - Rougamo Release 1.1.0
静态文件快速建站
Tanabata festival coming, VR panoramic look god assists for you
Sticker Spelling - Memory Search / Shape Pressure DP
jmeter跨平台运行csv等文件