在Python中的4个数字列表中查找2个中间数字

Ami*_*zan -1 python python-2.7

我想编写一个脚本,获取4个数字并打印2个中间数字.

我试了一下,但效果不好:

a = (input('enter first num:'))
b = (input('enter second num:'))
c = (input('enter thirs num:'))
d = (input('enter forth num:'))

##a=float(a)
##b=float(b)    
##c=float(c)
##d=float(d)

List=[a,b,c,d]
L=[]
for i in List:
        if i >> min(List):
            if i << max(List):
              L.append(i)
print L
Run Code Online (Sandbox Code Playgroud)

我不确定问题是什么,因为输出列表不连贯,实际上取决于输入.

我想得到一个线索或想法如何解决这个问题(仅使用列表,for和if - 这是非程序员的基础课程)

Sir*_*lot 5

这应该工作.它在列表中找到最小和最大元素,并将它们从列表中删除,留下中间两个元素.

a = int(input('enter first num:'))
b = int(input('enter second num:'))
c = int(input('enter thirs num:'))
d = int(input('enter forth num:'))

L = [a, b, c, d]

minimum = a
maximum = d
for i in L:
    if i < minimum:
        minimum = i
    if i > maximum:
        maximum = i
tmp = L[:]
tmp.remove(minimum)
tmp.remove(maximum)
print tmp
Run Code Online (Sandbox Code Playgroud)

注意:这只会删除最小和最大数字,如果列表大小为4,则只会留下两个数字

这是使用sort函数执行此操作的另一种方法,该函数对列表进行排序,然后您可以按索引获取元素.

a = int(input('enter first num:'))
b = int(input('enter second num:'))
c = int(input('enter thirs num:'))
d = int(input('enter forth num:'))

L=[a,b,c,d]

L.sort()
print L[1]
print L[2]
Run Code Online (Sandbox Code Playgroud)

如果您想将它们添加到列表中,您可以这样做

new_L = [L[1], L[2]]
Run Code Online (Sandbox Code Playgroud)