【问题标题】:List slicing and finding second highest value python列表切片并查找第二高值python
【发布时间】:2014-08-03 12:21:15
【问题描述】:

我需要编写一个以列表为参数的函数,并在列表中找到第二高的值。返回该值。如果列表中只有一个值,则返回该值。如果列表为空,则返回 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 的结束索引而不是 len 的长度。

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

[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] 并且我的 loc 是 6 并且长度是 4,我应该得到 [4, 6, 8, 10],但切片的第二高值是 9,这是第二高的用户列表,而不是切片。

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

编辑到编辑:

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

【问题讨论】:

  • 不要不必要地eval,这会导致邪恶的事情发生。
  • 我认为在我的代码中没有必要,除非有其他方法可以获取用户输入的数字
  • @devnull tr​​ue, @annabananana7 试试ast.literal_eval
  • 旁白:这与您的主要问题无关,但重复您的“输入数字”文本是一种常见的反模式。您可以阅读this question 了解原因。
  • @DSM 谢谢!下次我会尽量记住这一点:)

标签: python list python-3.x slice


【解决方案1】:

关于良好 Python 实践的几点说明:

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

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

当您对可迭代对象(如列表)进行切片时,您可以将切片留空,这样新列表所需的内容就会很聪明。例如,userList[loc:],或 userList[:],或 userList[:3]。

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

【讨论】:

  • 很好的建议,我真的明白了哈哈。谢谢,切片现在可以了:)
【解决方案2】:

试试这个以获得第二高:

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
>>> 

小贴士:

不要使用evaleval 是邪恶的。如果您必须使用类似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 和/或接受答案吗?谢谢!
  • 就像一个小评论:sorted(set(selSlice))[-2] 在更大的列表中可能比sorted(selSlice)[-2] 快很多,因为set 消除了所有重复项
【解决方案3】:

切片很简单:

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 个最高(或最低)值的非常快的方法

【讨论】:

  • -1 排序列表成本 o(nlog(n)),同时找到最大值仅成本 o(n)
  • @heroandtn3:查看时间以了解这并不总是正确的。
【解决方案4】:

从列表中删除项目的另一种方法是使用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多