0

PyScripter でプログラムを実行すると、予想される の中央値が得られ4.5ます。しかし、ここと同じ Ideoneまたはcodeacademyで同じコードを実行すると、が返されます4。なぜ異なる結果が得られるのか考えていますか? ありがとう。

#Create a function that returns the median of a list of numbers
def median(list_of_numbers):

#Make a copy of list_of_numbers and sort the new list
    lst = list_of_numbers
    lst = sorted(lst)

#Get the length of list and use it to index through the list
    lst_length = len(lst)
    index = int(lst_length/2)

#If length if even, average the two middle numbers
    if lst_length%2==0:
        a=  lst[index-1]
        b = lst[index]
        result = (a+b)/2

#If length is odd, return the middle number
    else:
        result = lst[index]
    return result

print (median([4, 5, 5, 4]))

私の PyScripter のバージョン: * Python 3.3.5 (v3.3.5:62cf4e77f785、2014 年 3 月 9 日、10:37:12) [MSC v.1600 32 ビット (Intel)] on win32。*

4

2 に答える 2

1

Python 2.x では、浮動小数点除算を強制するために、両方の2byを置き換える必要があります。2.

ideone へのリンクは Python 2.x であり、codeacademy インタープリターもそうです。

于 2014-06-27T03:01:30.550 に答える
0
import statistics as s

def mid(data):
    return(int(s.median(data)))

middle = mid([3,4,6,3,12,6,45,32,78])
print(middle)

統計モジュールを使用します。出力を整数または浮動小数点数にしたい場合は、キャストするだけです (それぞれ float() または int())。

于 2016-01-15T13:58:34.203 に答える