Sujet : Re: Baby X is bor nagain
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.lang.cDate : 18. Jun 2024, 21:07:05
Autres entêtes
Message-ID : <6671e8e9$0$2385535$882e4bbb@reader.netnews.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : Betterbird (Windows)
On 6/18/2024 3:52 PM, Mark Bourne wrote:
DFS wrote:
On 6/18/2024 12:11 PM, David Brown wrote:
>
>
if n % 2 == 1 :
median = sorted(data)[n // 2]
else :
median = sum(sorted(data)[(n // 2 - 1) : (n // 2 + 1)]) / 2
>
I think your else formula (n % 2 == 0) is incorrect:
>
n = 4
data = [1,2,3,4]
median = 2.5
>
Yours appears to sum (1,2,3) = 6 / 2 = 3.
>
Am I reading it correctly?
Python ranges include the start index but exclude the end index. So data[1:3] gives the items at data[1] and data[2], but not data[3]. Indexes are zero-based, so data[1:3] == [2, 3], sum([2, 3]) == 5, and 5 / 2 == 2.5.
I knew python is index-0 based and I've done a lot of string slicing, but I didn't know you could sum a slice.
Thanks.