zoukankan      html  css  js  c++  java
  • 同时对数据做转换和换算

    1.如果需要对数据进行换算,例如sum(),min(),max(),但是首先得对数据做转换和筛选,,有一个很优雅的方式解决

    score= [5, 7, 18, 19, 29, 39, 40, 41, 51, 57]
    s=sum(x*x for x in score)
    print(s)

    2.实际应用于一些题目测评答案分值的计算

    answer={
        "1":0,
        "2":1,
        "3":0,
        "4":1,
        "5":0,
        "6":1,
        "7":0,
        "8":1,
        "9":0,
        "10":1,
        "11":0,
        "12":1,
        "13":0,
        "14":1,
        "15":0,
        "16":1,
        "17":0,
        "18":1,
        "19":0,
        "20":1,
        "21":0,
        "22":1,
        "23":0,
        "24":1,
        "25":0,
        "26":1,
        "27":0,
        "28":1,
        "29":0,
        "30":1,
        "31":0,
        "32":1,
        "33":0,
        "34":1,
        "35":0,
        "36":1,
        "37":0,
        "38":1,
        "39":0,
        "40":1,
        "41":0,
        "42":1,
        "43":0,
        "44":1,
        "45":0,
        "46":1,
        "47":0,
        "48":1,
        "49":0,
        "50":1,
        "51":0,
        "52":1,
        "53":0,
        "54":1,
        "55":0,
        "56":1,
        "57":0,
        "58":1,
        "59":0,
        "60":1
    }
    testQuestions= [{'_id': '常规型(C)', 'score': [5, 7, 18, 19, 29, 39, 40, 41, 51, 57], 'max': 10}, {'_id': '社会型(S)', 'score': [1, 12, 15, 26, 27, 37, 45, 52, 53, 59], 'max': 10}, {'_id': '企业型(E)', 'score': [3, 11, 16, 24, 25, 28, 35, 38, 46, 60], 'max': 10}, {'_id': '研究型(I)', 'score': [6, 8, 20, 21, 30, 31, 42, 55, 56, 58], 'max': 10}, {'_id': '现实型(R)', 'score': [2, 13, 14, 22, 23, 36, 43, 44, 47, 48], 'max': 10}, {'_id': '艺术型(A)', 'score': [4, 9, 10, 17, 32, 33, 34, 49, 50, 54], 'max': 10}]
    #需求是对testQuestions列表中score列表中题号找到answer中对应的值,计算出_id 的总分数,说明白点就是算_id 的分值,现在我的score字段只是存着题号,要根据题号,去answer中找到分数累加,
    #我的方法阿是:
    for item in testQuestions:
        s=sum(answer[str(x)] for x in item["score"])
        print(s)
        item["score"]=s
    #输出结果testQuestions:{'_id': '常规型(C)', 'score': 2, 'max': 10}, {'_id': '社会型(S)', 'score': 3, 'max': 10}, {'_id': '企业型(E)', 'score': 6, 'max': 10}, {'_id': '研究型(I)', 'score': 7, 'max': 10}, {'_id': '现实型(R)', 'score': 6, 'max': 10}, {'_id': '艺术型(A)', 'score': 6, 'max': 10}]
  • 相关阅读:
    MATLAB 模板匹配
    ACDSee15 教你如何轻松在图片上画圈圈、画箭头、写注释
    Qt 显示一个窗体,show()函数和exec()函数有什么区别?
    Qt 将窗体变为顶层窗体(activateWindow(); 和 raise() )
    Qt QSS样式化 菜单Qmenu&QAction
    Qt 获取文件夹中的文件夹名字
    Qt 删除文件夹或者文件
    欧洲终于承认“工业4.0”失败,互联网经济严重落后中美
    深入浅出数据结构
    浅谈城市大脑与智慧城市发展趋势
  • 原文地址:https://www.cnblogs.com/zzy-9318/p/9970513.html
Copyright © 2011-2022 走看看