error :list indices must be integers, not float for median

前端 未结 2 384
别跟我提以往
别跟我提以往 2021-01-28 06:04

Trying to find median but keep getting list indices must be integers, not float error and am not sure what to do.

sorted_data = sorted(data, key=lambda d:d.a         


        
相关标签:
2条回答
  • 2021-01-28 06:42

    If you are using Python3, len(data)/2 will return you a float if len(data) is odd. Use // instead of / to get an integer result.

    0 讨论(0)
  • 2021-01-28 06:45

    The statistics module is part of the standard library:

    import statistics
    
    data = [1, 2, 3, 4]
    statistics.median(data)
    
    0 讨论(0)
提交回复
热议问题