returning python list indexes

Asked

Viewed 409 times

1

I’m having a problem on the line index, because it does not accept float. But how can I get around it?

def median(numbers):
    numbers.sort()#The sort method sorts a list directly, rather than returning a new sorted list
    num = len(numbers)
    hlf = int(num/2)
    if num%2 == 0:
        m1 = numbers[:hlf]
        m2 = numbers[hlf:]
        un = int(m1[-1])
        pn = int(m2[0])
        middle_index = float((un+pn)/2) #int(len(numbers)/2)
    else:
        middle_index = int(len(numbers)/2)
    return numbers[middle_index]

test1 = median([1,2,3])
print("expected result: 2, actual result: {}".format(test1))

test2 = median([1,2,3,4])
print("expected result: 2.5, actual result: {}".format(test2))

test3 = median([53, 12, 65, 7, 420, 317, 88])
print("expected result: 65, actual result: {}".format(test3))
  • You can’t understand your problem. Which error has appeared? On which line? What should this code do?

  • I don’t see how you can return a float when counting the number of indexes in a list, after all it is not possible to have 4.7 indexes, only integers.

  • this is the error that appears: "Typeerror: list indices must be integers, not float expected result: 2, current result: 2" , can’t have a broken index, but for this particular case we need to only divide between the sum of the two middle numbers. when a list has an even number of elements.

  • What exactly do you want?

  • whenever the list has an even number it must return a float, which is the division between the two numbers in the middle of the list with even number of elements.

2 answers

1


Whether your intention is to calculate the median of a list:

Numpy:

import numpy
numbers = [1,2,3,4]
numpy.median(numbers)
2.5

Numless

def median(numbers):
    sortlist = sorted(numbers)
    len_numbers = len(numbers)
    index = (len_numbers - 1) // 2
    if (len_numbers % 2):
        return sortlist[index]
    else:
        return (sortlist[index] + sortlist[index + 1])/2.0

median([1,2,3,4])
2.5
  • very elegant solution... thank you very much. I am learning fast with you.

1

From what I understand you are trying to implement the MEDIANA function.

In this case when the list of numbers has odd length you return the central element, but when the length of the list is even returns the average of the two central elements, correct?

If that’s what your implementation should be:

def median(numbers):
    numbers.sort()#The sort method sorts a list directly, rather than returning a new sorted list
    num = len(numbers)
    hlf = int(num/2)
    if num%2 == 0:
        mediana = (numbers[hlf-1] + numbers[hlf]) / 2 #Media dos dois elementos centrais
    else:
        mediana = numbers[hlf] #Elemento central
    return mediana

test1 = median([1,2,3])
print("expected result: 2, actual result: {}".format(test1))

test2 = median([1,2,3,4])
print("expected result: 2.5, actual result: {}".format(test2))

test3 = median([53, 12, 65, 7, 420, 317, 88])
print("expected result: 65, actual result: {}".format(test3))
  • a part worked Danilo, but now the odd lists are not returning the middle values.

  • Forget Danilo, gave it right... I had forgotten to put the line of the second median. Thanks anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.