1.已知一个数字列表,求列表中心元素。
games = [10,20,30,40,50,60,70,80,90,100]
while True:
if len(games) % 2 != 0:
print(games[(len(games)//2)])
break
else:
print(games[(len(games)//2)-1],games[(len(games)//2)])
break
2.已知一个数字列表,求所有元素和。
games = [10,20,30,40,50,60,70,80,90,100]
n = 0
for i in games:
n += i
print(n)
3.已知一个数字列表,输出所有奇数下标元素。
nums = [1,2,3,4,50,66,77,80,93,105]
for index in range(len(nums)):
if index % 2 != 0:
print(nums[index])
4.已知一个数字列表,输出所有元素中,值为奇数的元素。
nums = [1,2,3,4,50,66,77,80,93,105]
for i in nums:
if i % 2 != 0:
print(i)
5.已知一个数字列表,将所有元素乘二。
例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]
nums = [1, 2, 3, 4]
nums2 = []
for i in nums:
i *= 2
nums2 += [i]
print(nums2)
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = [‘张三’, ‘李四’, ‘大黄’, ‘大黄’, ‘张三’, ‘张三’, ‘张三’] -> names = [‘张三’, ‘李四’, ‘大黄’]
names = ['张三', '李四', '大黄', '大黄', '张三', '张三', '张三']
names2 = []
for index in names:
if not index in names2:
names2 += [index]
print(names2)
7.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
scores = [11, 23, 31, 42, 50, 66, 77, 80, 93, 105]
scores.sort()
del scores[0]
del scores[-1]
scores2 = 0
count = 0
for i in scores:
scores2 += i
count += 1
print(scores2/count)
8.有两个列表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 += [i]
print(C)
9.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)
例如: nums = [19, 89, 90, 600, 1] —> 600
nums = [19, 89, 90, 600, 1]
index = 0
for i in nums:
if i >= index:
index = i
print(index)