列出切片并找到第二高价值的python

时间:2014-06-13 02:40:26

标签: python list python-3.x slice

我需要编写一个函数,它将列表作为参数,并在列表中找到第二高的值。返回那个值。如果列表中只有一个值,则返回该值。如果列表为空,则返回0.

要创建列表,我会提示用户输入数字,直到用户输入-1的标记值。然后我提示用户输入起始位置(loc)和长度(len)。我将提取一个从索引loc开始并具有长度len的列表切片,然后使用我的函数来查找该列表切片中的第二个最高值。

这是我到目前为止的代码:

userList = []

def main():
    inputList = int(input("Enter a number >= 0. Enter -1 to end input: "))
    while inputList != -1:
        userList.append(inputList)
        inputList = eval(input("Enter a number >= 0. Enter -1 to end input: "))

    return extractList(userList)

def extractList(userList):
    loc = int(input("Enter a starting location: "))
    length = int(input("Enter a lenghth: "))

    selSlice = userList[loc:loc + length]

    if len(selSlice) == 1:
        return selSlice[0]
    if len(selSlice) == 0:
        return 0

    num = max(selSlice)
    occ = userList.count(num)
    for i in range(occ):
        userList[userList.index(num):userList.index(num)+1] = ''

    print("Second highest value in ", selSlice, "is ", max(selSlice))

main()

我正在测试切片是否有效,但它似乎采用了loc的起始索引并转到len的结束索引而不是走出le​​n的长度。

示例,如果我有一个列表:

[1, 3, 7, 21, 37, 76, 23, 91, 15, 11, 2, 4]

我的loc为3,len为6,结果应为[21, 37, 76, 23, 91, 15]。但是,我没有得到这个想要的结果,而是得到[21, 37, 76]

我的extractList(a)应该是什么?另外,如果你能帮助我找到第二个最高值的功能,那就太好了。感谢您的任何意见!

修改

好的,所以我现在正走在正确的轨道上,感谢Chris ArenaTo Click or Not to Click。 (代码已更新)

但是,上面的代码给出了整个列表的第二高值,而不是切片列表。我不确定所有变量是否正确。

如果我的userList[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]且我的位置是6且长度是4,我会回复[4, 6, 8, 10],但是切片的第二高值是9,这是userList的第二高,而不是切片。

我尝试将userList更改为selSlice,从第if len(userList) == 1:行开始直到结束,看看是否有所作为。确实如此,但结果值得怀疑(又名错误)。我使用了与前一段中提到的相同的userList,loc和length。我将[4, 6, 8]作为切片返回(错误),第二个最高值为8,这对于程序返回的切片是错误的,但对于我请求的切片是正确的。所以我不确定这里会出现什么问题。有什么建议吗?

修改为编辑:

我的最新代码显示正确的切片,但错误的第二高值。我越来越: Second highest value in [4, 6, 8, 10] is 10不确定需要修复的内容= \

4 个答案:

答案 0 :(得分:4)

尝试这个以获得第二高:

def extract(arr, start, finish):
    if start < 0 or finish > len(arr)-1:
        return "Please enter valid start/end points"
    lst = arr[start:finish]
    if len(lst) == 1:
        return lst[0]
    if len(lst) == 0:
        return 0
    num = max(lst)
    occ = lst.count(num)
    for i in range(occ):
        lst[lst.index(num):lst.index(num)+1] = ''
    return max(lst)

运行如下:

>>> extract([6, 7, 8, 9, 1, 5, 3, 7, 2], -1, 8)
'Please enter valid start/end points'
>>> extract([6, 7, 8, 9, 1, 5, 3, 7, 2], -3, 8)
'Please enter valid start/end points'
>>> extract([6, 7, 8, 9, 1, 5, 3, 7, 2], 3, 8)
7
>>> 

几点提示:

请勿使用eval eval是邪恶的。如果您必须使用eval之类的功能,请尝试ast.literal_eval(),或者只是投射int()

以下是您编辑的代码:

userList = []

def main():
    inputList = int(input("Enter a number >= 0. Enter -1 to end input: "))
    while inputList != -1:
        userList.append(inputList)
        inputList = eval(input("Enter a number >= 0. Enter -1 to end input: "))

    return extractList(userList)

def extractList(userList):
    loc = int(input("Enter a starting location: "))
    length = int(input("Enter a lenghth: "))

    selSlice = userList[loc:loc + length]
    orig = list(selSlice)

    if len(selSlice) == 1:
        return selSlice[0]
    if len(selSlice) == 0:
        return 0

    num = max(selSlice)
    occ = selSlice.count(num)
    for i in range(occ):
        selSlice[selSlice.index(num):selSlice.index(num)+1] = ''

    print("Second highest value in ", orig, "is ", max(selSlice))

main()

运行如下:

bash-3.2$ python3 test.py
Enter a number >= 0. Enter -1 to end input: 2
Enter a number >= 0. Enter -1 to end input: 7
Enter a number >= 0. Enter -1 to end input: 4
Enter a number >= 0. Enter -1 to end input: 9
Enter a number >= 0. Enter -1 to end input: 3
Enter a number >= 0. Enter -1 to end input: 1
Enter a number >= 0. Enter -1 to end input: 6
Enter a number >= 0. Enter -1 to end input: 4
Enter a number >= 0. Enter -1 to end input: 2
Enter a number >= 0. Enter -1 to end input: 8
Enter a number >= 0. Enter -1 to end input: 4
Enter a number >= 0. Enter -1 to end input: 2
Enter a number >= 0. Enter -1 to end input: 4
Enter a number >= 0. Enter -1 to end input: 3
Enter a number >= 0. Enter -1 to end input: 7
Enter a number >= 0. Enter -1 to end input: -1
Enter a starting location: 2
Enter a lenghth: 12
Second highest value in  [4, 9, 3, 1, 6, 4, 2, 8, 4, 2, 4, 3] is  8
bash-3.2$ 

另一种方法:

您可以使用sorted()并获取倒数第二个值:

userList = []

def main():
    inputList = int(input("Enter a number >= 0. Enter -1 to end input: "))
    while inputList != -1:
        userList.append(inputList)
        inputList = eval(input("Enter a number >= 0. Enter -1 to end input: "))

    return extractList(userList)

def extractList(userList):
    loc = int(input("Enter a starting location: "))
    length = int(input("Enter a lenghth: "))

    selSlice = userList[loc:loc + length]

    if len(selSlice) == 1:
        return selSlice[0]
    if len(selSlice) == 0:
        return 0

    num = sorted(selSlice)[-2]

    print("Second highest value in ", selSlice, "is ", num)

main()

运行方式:

bash-3.2$ python3 test.py
Enter a number >= 0. Enter -1 to end input: 2
Enter a number >= 0. Enter -1 to end input: 7
Enter a number >= 0. Enter -1 to end input: 4
Enter a number >= 0. Enter -1 to end input: 9
Enter a number >= 0. Enter -1 to end input: 3
Enter a number >= 0. Enter -1 to end input: 1
Enter a number >= 0. Enter -1 to end input: 6
Enter a number >= 0. Enter -1 to end input: 4
Enter a number >= 0. Enter -1 to end input: 2
Enter a number >= 0. Enter -1 to end input: 8
Enter a number >= 0. Enter -1 to end input: 4 
Enter a number >= 0. Enter -1 to end input: 2
Enter a number >= 0. Enter -1 to end input: 4
Enter a number >= 0. Enter -1 to end input: 3
Enter a number >= 0. Enter -1 to end input: 7
Enter a number >= 0. Enter -1 to end input: -1
Enter a starting location: 2
Enter a lenghth: 12
Second highest value in  [4, 9, 3, 1, 6, 4, 2, 8, 4, 2, 4, 3] is  8
bash-3.2$

答案 1 :(得分:1)

良好的Python实践很少:

最好在输入上调用int(),而不是eval()。由于各种原因,Eval是不安全的,如果你调用int(),如果用户给出一个错误值(例如字符串'blah'),它将立即抛出异常。然后,您可以捕获此异常,而不是将错误值添加到列表中。如果用户可以输入非整数,请改用float()。

您也不想覆盖内置名称,在本例中为“len”。为该变量选择一个不同的名称。

当您对迭代(例如列表)进行切片时,您可以将切片留空,并且它将清楚地了解新列表所需的内容。例如,userList [loc:],或userList [:],或userList [:3]。

最后,看起来你应该做的是获取起始位置并获取下一个“长度”的字符。因此,您需要使用userList [loc:loc + length] ...如果用户提供的值多于列表所包含的值,这也有可能失败,所以您也应该在这里进行一些错误检查。

答案 2 :(得分:0)

切片很简单:

li=[1, 3, 7, 21, 37, 76, 23, 91, 15, 11, 2, 4]

loc=3
run=6

sl=li[loc:loc+run]

print(sl)
# [21, 37, 76, 23, 91, 15]

然后是第二高:

print(sorted(set(sl))[-2])   # use 'set' in case there are duplicates...
# 76

找到SECOND max的各种方法的时间安排:

def f1(li):
    return sorted(set(li))[-2]

def f2(li):
    max_n=max(li)
    while max_n in li:
        li.remove(max_n)

    return max(li)

def f3(li):
    set_li=set(li)
    set_li.remove(max(set_li))
    return max(set_li)

def f4(li):
    max_n=max(li)
    li_=[x for x in li if x!=max_n]   
    return max(li_)

def f5(li):
    return sorted(li)[-2]    


if __name__ =='__main__':
    import timeit   
    import random
    li=[random.randint(0, 100) for _ in range(100000)]  
    for f in (f1, f2, f3, f4, f5):
        print('{}: {:.4} secs'.format(f.__name__,
                   timeit.timeit("f(li[:])", setup="from __main__ import li, f", number=10)))

打印:

f1: 0.01876 secs
f2: 14.61 secs         # OUCH
f3: 0.01861 secs
f4: 0.09974 secs
f5: 0.2418 secs

您可以看到排序唯一值是获得第N个最高(或最低)值的快速方法

答案 3 :(得分:0)

从列表中删除项目的另一种方法是使用remove(value)方法,例如:

while maxValue in lst:
    lst.remove(maxValue)

此外,最好编写一个单独的函数来从列表中查找第二高的值。请注意,调用此函数后将修改lst

def extractSecondHighest(lst):
    """
    Extraction second highest value from lst.
    """
    #lst = lst[:] <-- uncomment this line if you don't want `lst` to be modified 
    if len(lst) == 1:
        return lst[0]
    if len(lst) == 0:
        return 0

    maxValue = max(lst) # obtain max value
    while maxValue in lst: # while lst has maxValue, remove it
        lst.remove(maxValue)
    return max(lst)

然后,您的extractList功能将是:

def extractList(userList):
    loc = int(input("Enter a starting location: "))
    length = int(input("Enter a lenghth: "))

    selSlice = userList[loc:loc + length] # don't forget to check exception when loc + length is larger then list's length

    secondHighest = extractSecondHighest(selSlice)

    print("Second highest value in", userList, "is", secondHighest)