计算比例
Posted xiesongyou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算比例相关的知识,希望对你有一定的参考价值。
1 def read_data(): 2 with open("test","r",encoding="utf-8") as f : 3 for data in f: 4 yield data 5 6 g = read_data() 7 8 all_population = sum(eval(dt)["population"] for dt in g) 9 print(all_population ) 10 11 g1 = read_data() 12 13 for res in g1: 14 data_pic = eval(res) 15 print("%s所占全部的比例是%.1f"%(data_pic["city"],round(data_pic["population"]/all_population,3) *100))
文件如下:
{"city":"北京","population":56}
{"city":"上海","population":28}
{"city":"南京","population":30}
{"city":"安徽","population":40}
{"city":"台湾","population":100}
以上是关于计算比例的主要内容,如果未能解决你的问题,请参考以下文章
Vue3官网-高级指南(十七)响应式计算`computed`和侦听`watchEffect`(onTrackonTriggeronInvalidate副作用的刷新时机`watch` pre)(代码片段