首页 > 其他 > 详细

求中位数

时间:2019-06-02 21:42:55      阅读:110      评论:0      收藏:0      [点我收藏+]

|--1.python实现

 1 #!/usr/bin/python3
 2 
 3 def calc_num(nums):
 4 
 5     #nums_1 = sorted(nums)
 6     if not nums:
 7         return None
 8         
 9     nums_1 = sorted(nums)
10     
11     le = len(nums_1)
12     
13     if le % 2 == 0:
14         return ((nums_1[le // 2 - 1] +nums_1[le // 2]) / 2 )
15 
16     else:
17         return (nums_1[le // 2])
18 
19 
20 nums = [3, 33, 989, 20, 7, 8]
21 
22 print(calc_num(nums))                  

|---测试用例

  

1     b = [
2             {id: 101, rating: 5.0},
3             {id: 102, rating: 1.0},
4             {id: 102 rating: 2.0},
5             {id: 102, rating: 1.0},
6         ]

  思考如何使用快排算法实现

  

 

求中位数

原文:https://www.cnblogs.com/xuzhaoping/p/10964494.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!