python期末练习:列表
目录
1. 列表的简单运算
ls=eval(input())
count_odd=0
count_even=0
sum_odd=0
sum_even=0
for i in ls:
if i%2==1:
count_odd+=1
sum_odd+=i
else:
count_even+=1
sum_even+=i
avg=sum(ls)/len(ls)
print(count_odd,count_even,sum_odd,sum_even,avg)
2. 两两相乘再相加
ls=eval(input())
result = 0
for i in range(len(ls)):
for j in range(i+1, len(ls)):
result += ls[i] * ls[j]
print(result)
3. 考试成绩优秀
ls=eval(input())
count=0
for i in ls:
if i>85:
count+=1
avg=sum(ls)/len(ls)
print(count,avg)
4. 高于平均分
ls=[['唐三藏',98],
['孙悟空',88],
['猪八戒',46],
['沙和尚',62],
['白龙马',100]]
ls1=[]
sum1=0
for i in ls:
sum1+=i[1]
avg=sum1/len(ls)
for i in ls:
if i[1]>avg:
ls1.append(i[0])
print(ls1)
print(len(ls1))
5. 求双阶乘
ls1=eval(input())
ls2=[]
for i in ls1:
b=1
for i in range(i,0,-2):
#从i开始,每次递减2,到0之前结束,保证了相同的奇偶,以及不会乘到0
b*=i
ls2.append(b)
print(ls2)
6. 低洼地
ls=eval(input())
count=0
for i in range(1,len(ls)-1):
if (ls[i]<ls[i-1] and ls[i]<ls[i+1]) or (ls[i]==ls[i-1] and ls[i]<ls[i+1]):
#注意出现平地时的情况,通过逻辑运算符描述,and的优先级高于or,此处括号可去,不影响运算
count+=1
print(count)