Python程序:找到大于平均值的列表项

创新互联python教程:

专注于为中小企业提供成都网站建设、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业元宝山免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

写一个 Python 程序来查找大于平均值的列表项。这个 python 示例允许我们输入列表项,然后我们会找到列表项的平均值。接下来,for 循环将迭代列表项,if 条件根据列表平均值检查每个项。最后,如果大于平均值,则打印该列表项。

listAvg = []
Number = int(input("Total Number of List Items = "))

for i in range(1, Number + 1):
    value = int(input("Enter the %d List Item = "  %i))
    listAvg.append(value)

total = sum(listAvg)
avg = total / Number

print('\nList Sum = {0} and Average = {1}'.format(total, avg))

print('Total List Items Greater than the List Average')
for i in range(len(listAvg)):
    if listAvg[i] > avg:
        print(listAvg[i], end = ' ')

这个 Python 示例使用 for 循环查找大于平均值的列表项。

listAvg = []
Number = int(input("Total Number of List Items = "))

total = 0
for i in range(Number):
    value = int(input("Enter the %d List Item = "  %(i+1)))
    listAvg.append(value)
    total = total + listAvg[i]

avg = total / Number

print('\nList Sum = {0} and Average = {1}'.format(total, avg))

print('Total List Items Greater than the List Average')
for i in range(len(listAvg)):
    if listAvg[i] > avg:
        print(listAvg[i], end = ' ')
Total Number of List Items = 7
Enter the 1 List Item = 22
Enter the 2 List Item = 9
Enter the 3 List Item = 11
Enter the 4 List Item = 18
Enter the 5 List Item = 29
Enter the 6 List Item = 139
Enter the 7 List Item = 44

List Sum = 272 and Average = 38.857142857142854
Total List Items Greater than the List Average
139 44 

Python 程序使用 while 循环查找大于平均值的列表项。

listAvg = []
Number = int(input("Total Number of List Items = "))

total = 0
i = 1
while(i <= Number):
    value = int(input("Enter the %d List Item = "  %(i+1)))
    listAvg.append(value)
    i = i + 1

i = 0
while(i < Number):
    total = total + listAvg[i]
    i = i + 1

avg = total / Number

print('\nList Sum = {0} and Average = {1}'.format(total, avg))

print('Total List Items Greater than the List Average')
for i in range(len(listAvg)):
    if listAvg[i] > avg:
        print(listAvg[i], end = ' ')
Total Number of List Items = 9
Enter the 2 List Item = 4
Enter the 3 List Item = 11
Enter the 4 List Item = 3
Enter the 5 List Item = 2
Enter the 6 List Item = 9
Enter the 7 List Item = 22
Enter the 8 List Item = 7
Enter the 9 List Item = 15
Enter the 10 List Item = 17

List Sum = 90 and Average = 10.0
Total List Items Greater than the List Average
11 22 15 17 

分享标题:Python程序:找到大于平均值的列表项
网址分享:http://www.shufengxianlan.com/qtweb/news38/495788.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联