1、输入一个整数列表,将列表中的元素按照逆序输出。
list1=[5,4,5,6]
list1.reverse()
print(list1)
[6, 5, 4, 5]
2、输入一个字符串列表,输出其中长度大于等于5的字符串,并且将它们转换为大写形式。
list1=['hello','lol','ak47','aliang']
for i in list1:
if len(i) >=5:
print(i.upper())
HELLO
ALIANG
3、输入一个整数列表,输出该列表中所有奇数的平方和。
list1=[1,2,3,4,5,6,7,8,9]
sum = 0
for num in list1:
if num % 2 == 1:
sum += num ** 2
print(sum)
165
4、输入一个列表,输出该列表中所有正整数的和。
list1=[-5,-1,0,1,2,3,1.7,'i']
sum = 0
for num in list1:
if isinstance(num, (int)) and num > 0 :
sum+= num
print(sum)
6
5、输入一个列表,输出该列表中所有偶数的平均值。
list1=[-5,-1,0,1,2,3,1.7,'i']
sum = 0
i = 0
for num in list1:
if isinstance(num, (int)) and num % 2 == 0 :
sum+= num
i+=1
print(sum/i)
1.0
6、已知一个数字列表,求所有元素和。
lst = [51, 54, 85, 45, 80, 45, 12, 96, 789, 45, 69]
lst = [51, 54, 85, 45, 80, 45, 12, 96, 789, 45, 69]
sum = 0
for numb in lst:
sum += numb
print(sum)
1371
7、已知一个数字列表,输出所有奇数下标元素。
lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
for j in range(len(lst)):
if j % 2 == 1:
print(lst[j])
54
45
45
96
45
8、已知一个数字列表,输出所有元素中,值为奇数的元素。
lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
for i in lst:
if i % 2 == 1:
print(i)
59
89
45
45
789
45
69
9、已知一个数字列表,将所有元素乘二。
nums = [1, 2, 3, 4]
nums = [1, 2, 3, 4]
for j in range(len(nums)):
nums[j] = nums[j] * 2
print(nums)
[2, 4, 6, 8]
10、有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = [‘张三’, ‘李四’, ‘大黄’, ‘张三’] -> names = [‘张三’, ‘李四’, ‘大黄’]
names = ['张三', '李四', '大黄', '张三','小黑','小白','小明','小红']
names = ['张三', '李四', '大黄', '张三', '小黑', '小白', '小明', '小红']
names_unique = []
for name in names:
if name not in names_unique:
names_unique.append(name)
print(names_unique)
['张三', '李四', '大黄', '小黑', '小白', '小明', '小红']
11、有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)
nums = [19, 89, 90, 600, 1]
nums = [19, 89, 90, 600, 1]
max_nums = 0
for i in nums:
if i > max_nums:
max_nums = i
print(max_nums)
600
12、用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]
scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]
min_score = 10
min_num = 0
max_score = 0
max_num = 0
for i in range(len(scores)):
if scores[i] > max_score:
max_score = scores[i]
max_num = i
if scores[i] < min_score:
min_score = scores[i]
min_num = i
del scores[min_num]
del scores[max_num]
sum_score = 0
for score in scores:
sum_score += score
avg_score = sum_score / len(scores)
print(avg_score)
9.5
scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]
# 对分数列表进行排序
scores.sort()
# 去掉一个最高分和一个最低分
scores = scores[1:-1]
sum_score = 0
for score in scores:
sum_score += score
avg_score = sum_score / len(scores)
print(avg_score)
9.5
scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]
scores.remove(max(scores))
scores.remove(min(scores))
score = sum(scores) / len(scores)
print(score)
9.5
13、有两个列表A和B,使用列表C来获取两个列表中公共的元素
例如: A = [1, ‘a’, 4, 90] B = [‘a’, 8, ‘j’, 1] --> C = [1, ‘a’]
A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
C = []
for i in A:
if i in B:
C.append(i)
print(C)
[1, 'a']
14、获取列表中出现次数最多的元素
nums = [1, 2, 3,1,4,2,1,3,7,3,3]
nums = [1, 2, 3,1,4,2,1,3,7,3,3]
res = 0
res_i = 0
for i in nums:
if nums.count(i) > res:
res = nums.count(i)
res_i = i
print(res_i)
3
15、将scores中所有的及格的分数提取出来
scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
scores_pass = []
for score in scores:
if score >= 60:
scores_pass.append(score)
print(scores_pass)
[89, 67, 90, 98, 78, 99]
16、输入一个整数列表,输出其中最大的两个元素的乘积。
lst=[2,6,7,10,3,5,4,9,8]
lst.sort(reverse=True)
print(lst[0]*lst[1])
90
17、已知一个数字列表,求所有元素的平均数
lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
sum_score = 0
for score in lst:
sum_score += score
avg_score = sum_score / len(lst)
print(avg_score)
125.54545454545455
18、输入一个字符串列表,输出其中最长的字符串。
lst = ['apple', 'banana', 'pear', 'orange', 'kiwi', 'mango']
lst = ['apple', 'banana', 'pear', 'orange', 'kiwi', 'mango']
res = []
num = 0
for k in lst:
if len(k) > num:
num = len(k)
res = [k]
elif len(k) == num:
res.append(k)
print(res)
['banana', 'orange']
19、输入一个字符串列表,输出其中第一个字母是元音字母的单词(不区分大小写)。
lst = ['apple', 'Banana', 'pear', 'Orange', 'kiwi', 'mango']
lst = ['apple', 'Banana', 'pear', 'Orange', 'kiwi', 'mango']
vowels = ['a','e','i','o','u','A','E','I','O','U']
for fruit in lst:
for char in vowels:
if fruit.startswith(char):
print(fruit)
apple
Orange
20、已有两个列表,输出它们的笛卡尔积。
lst1 = [1, 2, 3]
lst2 = ['a', 'b','c']
lst1 = [1, 2, 3]
lst2 = ['a', 'b','c']
for i in lst1:
for j in lst2:
print(i, j)
1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c