简体   繁体   English

计算平均值并在python中进行比较

[英]calculating an average and comparison in python

I have a set of values tgat represent the number of people in a postcodd on average. 我有一组值tgat表示平均一个后编码的人数。 These are pcode1 = 239 , pcode2 = 245 , pcode3 = 210 and pcode4 = 217 now comes the bit where I am stuck. 这些是pcode1 = 239pcode2 = 245pcode3 = 210pcode4 = 217现在是我所困的地方。 I have assumed that the average is allpcode = 220 . 我假设平均值是allpcode = 220

Is there a way to write a code in python such that I can compare each individual pcode to the total average? 有没有办法在python中编写代码,这样我就可以将每个单独的pcode与总平均值进行比较? What I want to do exactly is say compare pcode1 to allpcode and if pcode1 is higher/lower than allpcode display the average from pcode1 and say that pcode1 is lower than allpcode or higher than allpcode . 我想要做的恰恰是说比较pcode1allpcode如果pcode1高于/低于allpcode显示了从平均pcode1 ,说pcode1低于allpcode小于或更高allpcode I'd then like to repeat for all other pcodes :) 然后,我想对所有其他pcode重复:)

Id like to display on screen all the averages that are lower and all averages that are higher. 我想在屏幕上显示所有较低的平均值和较高的所有平均值。

So something like pcode 3 and 4 averages are lower than all pcode average and pcode 1 and 2 are higher than all p code. 因此,类似pcode 3和4的平均值低于所有pcode平均值,而pcode 1和2则高于所有p代码。

Is it possible to create such a code because i have no idea how/where to start? 是否可以创建这样的代码,因为我不知道如何/从哪里开始?

In cases like this, where you frequently want to iterate over all of a set of values, it's generally better to use a single list , dict , or other collection variable, instead of a bunch of separate variables. 在这种情况下,您经常要遍历一组值的所有位置,通常最好使用单个listdict或其他collection变量,而不是一堆单独的变量。 For example: 例如:

pcodes = {1: 239, 2: 245, 3: 210, 4: 217}

Now you can write code like this: 现在您可以编写如下代码:

pcodes = {1: 239, 2: 245, 3: 210, 4: 217}
allpcode = 220

for pcode, value in pcodes.items():
    if value < allpcode:
        print('code {} is lower than allpcode'.format(pcode))
    elif value > allpcode:
        print('code {} is higher than allpcode'.format(pcode))

If you really want to have separate variables, you have to do more work, like this: 如果您确实想拥有单独的变量,则必须做更多的工作,如下所示:

pcode1 = 239
pcode2 = 245
pcode3 = 210
pcode4 = 217
allpcode = 220

for i, value in enumerate((pcode1, pcode2, pcode3, pcode4)):
    if value < allpcode:
        print('code {} is lower than allpcode'.format(i + 1))
    elif value > allpcode:
        print('code {} is higher than allpcode'.format(i + 1))

You can use a list if you only want to match values: 如果只想匹配值,则可以使用列表:

>>> pcode1 = 239
>>> pcode2 = 245
>>> pcode3 = 210
>>> pcode4 = 217
>>> lis = [pcode1, pcode2, pcode3, pcode4]
>>> allpcode= 220

>>> for i,x in enumerate(lis):
    if x < allpcode:
        print "pcode{} is less than {}".format(i+1,allpcode)
    elif x > allpcode:    
        print "pcode{} is greater than {}".format(i+1,allpcode)
...         
pcode1 is greater than 220
pcode2 is greater than 220
pcode3 is less than 220
pcode4 is less than 220

Better use dictionary: 更好地使用字典:

As variables are just references to values in python so you can't access a variable name. 由于变量只是对python中值的引用,因此您无法访问变量名。 And in case you've lots of pcodes then defining a variable for each of them is bad way to solve this problem, using a dict is going to be much cleaner. 如果您有很多pcodes那么为每个pcodes定义一个变量是解决此问题的不好方法,那么使用dict会更干净。

#create dictionary with keys named pcode1, pcode2,...     
>>> dic = {'pcode1':239, 'pcode2':245, 'pcode3':210, 'pcode4':217}

>>> for k,v in dic.items():
    if v < allpcode:
        print "{} is less than {}".format(k ,'allpcode')
    elif v > allpcode:    
        print "{} is greater than {}".format(k,'allpcode')
...         
pcode3 is less than allpcode
pcode2 is greater than allpcode
pcode1 is greater than allpcode
pcode4 is less than allpcode

Though dictionaries don't maintain any order, you may have to collections.OrderedDict or sorted here to get keys in a particular order. 尽管字典不维护任何顺序,但您可能必须collections.OrderedDict或在此处sortedsorted特定顺序获取键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM