对于循环/平均价格

时间:2015-10-23 08:47:32

标签: python python-3.x

from collections import namedtuple
Car = namedtuple('Car','name year mpg price')

C1 = Car('Toyota', 1985, 21, 1000)
C2 = Car('Honda', 1994, 29, 2400)
C3 = Car('Kia', 2003, 26, 6000)

CL = [C1, C2, C3]

我需要创建一个函数,我可以找到所有汽车的平均价格(给定任何元组列表)

def sum_price(CL):
    for i in CL:
        print(sum(i.price))

^我知道这并不适用于总和...但我不知道该怎么做

Sum/range(len(CL))

^我知道我应该实现类似的东西来找到平均值

我该如何处理?我是否必须让sum_price(CL)函数打印一个列表,以使sum()运算符起作用?

3 个答案:

答案 0 :(得分:5)

你使用sum()走在正确的轨道上,但是你应该把迭代放在其中,这样它实际上可以添加一些内容:

def avg_price(CL):
    print(sum(i.price for i in CL) / len(CL))

要查找平均值,只需除以list的长度即可。您不需要将所有数字除以0到list的长度(不包括)。

我会在这里使用return

def avg_price(CL):
    return sum(i.price for i in CL) / len(CL)

然后将通话置于print()电话:

print(avg_price(CL))

答案 1 :(得分:1)

您可以在sum函数调用中使用列表推导,即

body {font-family: palatino linotype;
background: url(rngbackground.png) repeat 0 0;

}

p{
color:black;
font-family: palatino linotype;
}

h2{
color:grey;
}

h5{
color:grey;
}


a{
color:black;
text-decoration:none;

}

a:hover{
color:blue;

}

a:active{
color:black;
}

a:visited{
color:none;
text-decoration:none
}

#home{
list-style-type: none;
margin: 10px;
padding: 10px; 
border:1px solid grey;
width: 18.6%;
text-align:center;
font-family: Palatino Linotype;
font-size: 15px;
-moz-appearance: none;
-moz-box-shadow: 0 3px 0 #ccc, 0 -1px #2961c1 inset;
box-shadow: 0 3px 0 #ccc, 0 -1px #2961c1 inset;
text-transform:uppercase;
background:white;
height:2%;
position:absolute; top: 15%; left: 2%;

}

#dropdown:hover ul#dropdown-list{
display:block;
border:1px solid grey;

}
#dropdown ul{
padding:0px;
list-style: none;

}
#dropdown ul li{
width: 100%;
}
#dropdown ul li:hover{
background-color: lightblue;
cursor:pointer;
font-family: Palatino Linotype;
  font-size: 15px;
  -moz-appearance: none;
  -moz-box-shadow: 0 3px 0 #ccc, 0 -1px #2961c1 inset;
  box-shadow: 0 3px 0 #ccc, 0 -1px red inset;
}
#dropdown ul li a{  
padding-left: 10px;
text-decoration:none;
}


/* end 1*/

#dropdown2:hover ul#dropdown-list{
display:block;
border:1px solid grey;

}
#dropdown2 ul{
padding:0px;
list-style: none;

}
#dropdown2 ul li{
width: 100%;
}
#dropdown2 ul li:hover{
background-color: lightblue;
cursor:pointer;
font-family: Palatino Linotype;
  font-size: 15px;
  -moz-appearance: none;
  -moz-box-shadow: 0 3px 0 #ccc, 0 -1px #2961c1 inset;
  box-shadow: 0 3px 0 #ccc, 0 -1px red inset;
}
#dropdown2 ul li a{  
padding-left: 10px;
text-decoration:none;
}

/* end 2*/

#dropdown-list{
width: 100%;

display:none;
padding:0px;
margin:0px;
font-family: Palatino Linotype;
  font-size: 15px;
  -moz-appearance: none;

   background-color:white;
 }



#dropdown{

border: 1px solid lightgray;
 padding:5px;
 margin:0px;
 cursor:pointer;
 width:19.25%;
 height:3%;
 text-align:center;
 position:absolute;top:24%;left:2.6%;
 font-family: palatino linotype;
 font-size: 15px;
 -moz-appearance: none;
-moz-box-shadow: 0 3px 0 #ccc, 0 -1px #2961c1 inset;
box-shadow: 0 3px 0 #ccc, 0 -1px red inset;
background-color:white;
z-index:+10;
}


#dropdown2 { 
border: 1px solid lightgray;
margin: 10px;
padding: 10px; 
width: 18%;
text-align:center;
font-family: Palatino Linotype;
font-size: 15px;
-moz-appearance: none;
-moz-box-shadow: 0 3px 0 #ccc, 0 -1px #2961c1 inset;
box-shadow: 0 3px 0 #ccc, 0 -1px red inset;

position:absolute;top:31%;left:2%;
font-family: palatino linotype;
font-size: 15px;
-moz-appearance: none;
-moz-box-shadow: 0 3px 0 #ccc, 0 -1px #2961c1 inset;
box-shadow: 0 3px 0 #ccc, 0 -1px red inset;
background-color:white;
z-index:+9;
}

这在功能上是等同的

sum([i.price for i in CL])

答案 2 :(得分:0)

在python 3.4及更高版本中使用内置statistics模块和mean function

>>> from statistics import mean
>>> from itertools import chain
>>> print(mean(car.price for car in CL))
3133.3333333333335