Liste des Groupes | Revenir à cl c |
DFS wrote: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.On 6/18/2024 12:11 PM, David Brown wrote: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.
>
>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?
Les messages affichés proviennent d'usenet.