python - Python删除最低值
问题描述
这是我必须翻译成 python 的伪代码
A=99
LENGTH= LENGTH(list)
LIST= 92 50 26 82 73
for P in range 0 to LENGTH-1
IF LIST[P] <A THEN
A=LIST[P]
B=P
ENDIF
ENDFOR
IF B < LENGTH THEN
for P in range B to LENGTH -2
LIST[P] = LIST[P+1]
ENDFOR
ENDIF
LENGTH=LENGTH-1
LIST[LENGTH]=NULL
我尝试在下面进行编码,该代码旨在从 LIST 中删除最小值
a = 99
list=[92,50,26,82,73]
for p in range (0,len(list) - 1):
if list[p] < a :
a = list[p]
b = p
print (list) #I just added this to see what was happening
if b < len(list):
for p in range (b,len(list)-2):
list[p]=list[p]+1
list=len(list)-1
print (list)
#I just added this to see what was happening
我已经编写了上面的代码,它不会删除最低值
解决方案
你真的很接近,这里是更正:
A = 99
l = [92, 50, 26, 82, 73]
LENGTH = len(l)
for P in range(LENGTH):
if l[P] < A:
A=l[P]
B=P
if B < LENGTH:
for P in range (B, LENGTH - 1):
l[P] = l[P+1]
LENGTH=LENGTH-1
l[LENGTH]=None
现在试试:
print(l) # [92, 50, 82, 73, None]
注意:
LENGTH-1
andLENGTH-2
更改为LENGTH
andLENGTH-1
因为 Python 使用基于 0 的索引。
推荐阅读
- flutter - 无法在 mac 上升级颤振
- angular - 使用默认值重置表单
- linux - 如何在bash中闪烁光标之前粘贴文件的内容?
- apache-kafka - 将 Kafka 1 更新为 Kafka 2
- php - 比嵌套 SELECT 语句 SQL 更好的方法
- r - 在本地使用 R 运行 git 存储库
- .net-core - 如何使用 OpenID 处理 .net 核心中的访问被拒绝?
- c# - 如何有条件地在 RX 中缓冲
- python - 如何修复以下有关获取 url 的错误
- azure-functions - 您的 azure 函数应用将 functions_worker_runtime 设置为节点,而您的本地项目设置为 none