m0_58318154 2021-05-16 22:12 采纳率: 75%
浏览 171
已采纳

python列表中位数

定义一个求数值型数据列表中位数的函数median(lst),参数为一个包含若干个数值型数据的简单列表,函数返回中位数的值。 image.png 主程序首先通过input 以嵌套列表形式输入一个m*m的整数矩阵,如 [ [1,12,3,-2] , [14,5,26,9] , [18,9,10,-7] , [61,112,13,-12] ],然后调用自定义median函数循环求解每行数据的中位数,生成一个含有m个元素的中位数列表。最后输出中位数列表的中位数,以及中位数列表中的所有整数元素(即 小数部分为0的元素)组成的列表(循环中使用匿名函数实现,参照课本131页内容) 输入描述 [[1,7,4,6],[5,9,4,2],[88,3,3,1],[88,31,12,1]] 输出描述 The median of medians is 4.75 and The list of integer median is [5.0, 3.0] 样例输入1: [[1,7,4,6],[5,9,4,2],[88,3,3,1],[88,31,12,1]] 样例输出1: The median of medians is 4.75 and The list of integer median is [5, 3] 样例输入2: [[11,2,13],[22,1,12]] 样例输出2: The length of sub_list must be same to main_list! Please check your input ! 语言 Python 代码(请注意:不要出现中文)

  • 写回答

3条回答 默认 最新

  • benbenli 2021-05-17 02:10
    关注
    
    
    s=input("Please enter m * m matrix in the form  of list of list of integers in a line: ")
    
    #s = "[ [1,12,3,-2] , [14,5,26,9] , [18,9,10,-7] , [61,112,13,-12] ]"
    #s = " [[1,7,4,6],[5,9,4,2],[88,3,3,1],[88,31,12,1]] "
    
    def textToList(t):
        ta = t.replace(" ", "").split( "],[")
        taa = [x.replace("[","").replace("]","").split(",") for x in ta]
        iaa =[[int(y) for y in x] for x in taa]
        return iaa
    
    def calc_median(lst):
        lst.sort()
        l = len(lst)
        if l % 2 == 0:
            return (lst[int(l/2) - 1] + lst[int(l/2)]) / 2
        else:
            return lst[int(len(l) / 2)]
    
    lst = textToList(s)
    
    m = len(lst)
    
    valid = True
    medians = []
    
    for i in range(0, m):
        l = lst[i]
        if (len(l) != m):
            print(f"The length of sub_list {i} is {l} but it must be same to main_list {m}! Please check your input!")
            valid = False
            break
        medians.append(calc_median(l))
    
    if valid:
        mi = [x for x in medians if int(x) == x]
        mm = calc_median(medians)
        print(f"The median of medians is {mm} and The list of integer median is {mi}")
        
    
    
    # Output
    Please enter m * m matrix in the form  of list of list of integers in a line: [ [1,12,3,-2] , [14,5,26,9] , [18,9,10,-7] , [61,112,13,-12] ]
    The median of medians is 10.5 and The list of integer median is [2.0, 37.0]
    
    Please enter m * m matrix in the form  of list of list of integers in a line: [[1,7,4,6],[5,9,4,2],[88,3,3,1],[88,31,12,1]]
    The median of medians is 4.75 and The list of integer median is [5.0, 3.0]
    
    Please enter m * m matrix in the form  of list of list of integers in a line:  [[11,2,13],[22,1,12]]
    [[11,2,13],[22,1,12]]
    The length of sub_list 0 is [11, 2, 13] but it must be same to main_list 2! Please check your input!
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛