[英]List slicing and finding second highest value python
我需要编写一个函数,它将列表作为参数,并在列表中找到第二高的值 。 返回那个值。 如果列表中只有一个值,则返回该值。 如果列表为空,则返回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 Arena和To Click或Not to Click 。 (代码已更新)
但是,上面的代码给出了整个列表的第二高值,而不是切片列表。 我不确定所有变量是否正确。
如果我的userList
是[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
userList
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
并且我的loc是6并且长度是4,那么我得到[4, 6, 8, 10]
4,6,8,10 [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,6,8,10]中的Second highest value in [4, 6, 8, 10] is 10
不确定需要修复= \\
尝试这个以获得第二高:
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$
优秀的Python实践很少:
你最好在输入上调用int()而不是eval()。 由于各种原因,Eval是不安全的,如果你调用int(),如果用户给出一个错误值(例如字符串'blah'),它将立即抛出异常。 然后,您可以捕获此异常,而不是将错误值添加到列表中。 如果用户可以输入非整数,请改用float()。
您也不想覆盖内置名称,在本例中为“len”。 为该变量选择其他名称。
当您对迭代(例如列表)进行切片时,您可以将切片留空,并且对于新列表所需的内容将是明智的。 例如,userList [loc:],或userList [:],或userList [:3]。
最后,看起来你应该做的就是占据起始位置并获取下一个“长度”的角色。 因此,您需要使用userList [loc:loc + length] ...如果用户提供的值多于列表保存的值,这也有可能失败,所以您也应该在这里进行一些错误检查。
切片很简单:
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个最高(或最低)值的快速方法
从列表中删除项目的另一种方法是使用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.