Python删除最低值

时间:2019-05-21 01:59:27

标签: python pseudocode

这是我必须转换为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

我已经编写了上面的代码,但并没有去除最小值

2 个答案:

答案 0 :(得分:0)

  • 第一个列表是错误的变量名。它已经在python中具有了意义。
  • 只需遍历列表项
for item in items:
    # use item. 
  • 如果您还想要索引
for idx in range(len(items)):   # no need for -1
    # use items[idx] 
  • 你的灵魂
minval = min(items)
new_items = []
for item in items:
    if item != minval:
        new_items.append(item)

# new items has the answer

答案 1 :(得分:0)

您真的很亲密,以下是更正内容:

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]

注意: 由于Python使用基于0的索引,因此LENGTH-1LENGTH-2分别更改为LENGTHLENGTH-1