当前位置:网站首页>实例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电子书!↓↓↓↓↓↓
"""边栏推荐
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- typescript53-泛型约束
- GNSS文章汇总
- Sky map coordinate system to Gaode coordinate system WGS84 to GCJ02
- 【正则表达式】笔记
- 工程制图复习题(带答案)
- Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
- Installation and configuration of nodejs+npm
- boot issue
- 通用的测试用例编写大全(登录测试/web测试等)
猜你喜欢

pygame 中的transform模块

idea中diagram使用

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

typescript52 - simplify generic function calls

IDEA02:配置SQL Server2019数据库

typescript51-泛型的基本使用

typescript50-交叉类型和接口之间的类型说明

nodejs+express realizes the access to the database mysql and displays the data on the page

特征值与特征向量

Observability:你所需要知道的关于 Syslog 的一些知识
随机推荐
html select标签赋值数据库查询结果
esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
typescript54-泛型约束
贴纸拼词 —— 记忆化搜索 / 状压DP
持续投入商品研发,叮咚买菜赢在了供应链投入上
字符串的排列
LeetCode third topic (the Longest Substring Without Repeating Characters) trilogy # 3: two optimization
lombok注解@RequiredArgsConstructor的使用
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
《The Google File System》新说
TensoFlow学习记录(二):基础操作
[store mall project 01] environment preparation and testing
Intranet penetration - application
可变字符串
【OpenCV】-重映射
Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
优秀的测试/开发程序员,是怎样修炼的?步步为营地去执行......
Jmeter cross-platform operation CSV files