Liste des Groupes | Revenir à cl c |
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 :I think your else formula (n % 2 == 0) is incorrect:
median = sorted(data)[n // 2]
else :
median = sum(sorted(data)[(n // 2 - 1) : (n // 2 + 1)]) / 2
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.