Python 查找列表中间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38130895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:22:56  来源:igfitidea点击:

Find middle of a list

python

提问by Alastair

How would I find the exact middle of a python list?

我如何找到 python 列表的确切中间位置?

aList = [1,2,3,4,5]
middle = findMiddle(aList)
print middle

This is just an example of a function that would be able to find the middle of any list, is this something you can do using list comprehension?

这只是一个能够找到任何列表中间的函数的例子,你可以使用列表理解来做这件事吗?

Edit: This is different than taking out the middle point because I would like to just print out the middle value, if the list is odd I would like to return both the middle values like the accepted answer. Not getting the median like the other question has asked and getting the average of the two values.

编辑:这与取出中间点不同,因为我只想打印出中间值,如果列表是奇数,我想像接受的答案一样返回两个中间值。没有像其他问题那样获得中位数并获得两个值的平均值。

采纳答案by Kyle Baker

Why would you use a list comprehension? A list comprehension only knows about any one member of a list at a time, so that would be an odd approach. Instead:

为什么要使用列表理解?列表推导式一次只知道列表中的任何一个成员,因此这将是一种奇怪的方法。反而:

def findMiddle(input_list):
    middle = float(len(input_list))/2
    if middle % 2 != 0:
        return input_list[int(middle - .5)]
    else:
        return (input_list[int(middle)], input_list[int(middle-1)])

This one should return the middle item in the list if it's an odd number list, or a tuple containing the middle two items if it's an even numbered list.

如果它是一个奇数列表,那么这个应该返回列表中的中间项,或者如果它是一个偶数列表,则返回一个包含中间两项的元组。

Edit:

编辑:

Thinking some more about how one could do this with a list comprehension, just for fun. Came up with this:

想更多关于如何使用列表理解来做到这一点,只是为了好玩。想出了这个:

[lis[i] for i in 
    range((len(lis)/2) - (1 if float(len(lis)) % 2 == 0 else 0), len(lis)/2+1)]

read as:

读作:

"Return an array containing the ith digit(s) of array lis, where i is/are the members of a range, which starts at the length of lis, divided by 2, from which we then subtract either 1 if the length of the list is even, or 0 if it is odd, and which ends at the length of lis, divided by 2, to which we add 1."

“返回一个包含 array 的i第 th 位数字的数组lis,其中 i 是一个范围的成员,从 开始the length of lis, divided by 2, from which we then subtract either 1 if the length of the list is even, or 0 if it is odd,到 结束the length of lis, divided by 2, to which we add 1。”

The start/end of range correspond to the index(es) we want to extract from lis, keeping in mind which arguments are inclusive/exclusive from the range()function in python.

范围的开始/结束对应于我们想要从中提取的索引lis,记住哪些参数是包含/不包含range()在 python 中的函数的。

If you know it's going to be an odd length list every time, you can tack on a [0]to the end there to get the actual single value (instead of an array containing a single value), but if it can or will be an even length list, and you want to return an array containing the two middle values OR an array of the single value, leave as is. :)

如果你知道它每次都会是一个奇数长度的列表,你可以在[0]那里添加 a 以获得实际的单个值(而不是包含单个值的数组),但如果它可以或将是偶数长度列表,并且您想返回一个包含两个中间值的数组或单个值的数组,保持原样。:)

回答by heinst

Something like this would do:

像这样的事情会做:

aList = [1,2,3,4,5]
#minus 1 because the first element is index 0
middleIndex = (len(aList) - 1)/2
print middleIndex
print aList[middleIndex]

回答by Martin Lear

Take the length of the list, cut that in half and access whatever the list at that index point.

取列表的长度,将其切成两半并访问该索引点处的任何列表。

回答by Yiwen Shi

Are you expecting something like

你期待类似的东西吗

def findMiddle(list):
  l = len(list)
  if l/2:
    return (list[l/2-1]+list[l/2])/2.0
  else:
    return list[(l/2-1)/2]