day4作业

1.已知一个数字列表,求列表中心元素。

num = [78, 67, 52, 78, 99, 23]
n = len(num)
if n % 2 == 0:
	x = n // 2
	y = n // 2 -1
	print(num[x], num[y])
else:
	print(num[n//2])

2.已知一个数字列表,求所有元素和。

num = [78, 67, 52, 78, 99, 23]
total_num = 0
for s in num:
    total_num += s
print(total_num)

3.已知一个数字列表,输出所有奇数下标元素。

num = [78, 67, 52, 78, 99, 23]
total_num = 0
n = 0
for s in num:
    n += 1
    if  n & 1 == 1:
        total_num = s
        print(total_num, end = " ")

4.已知一个数字列表,输出所有元素中,值为奇数的元素。

sum = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
for i in range(0, len(sum)):
    if sum[i] % 2 == 1:
        print(sum[i])

5.已知一个数字列表,将所有元素乘二。

例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]

nums = [1, 2, 3, 4]
x = 0
for n in nums:
	nums[x] = n * 2
	x += 1
print(nums)

6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = [‘张三’, ‘李四’, ‘大黄’, ‘张三’] -> names = [‘张三’, ‘李四’, ‘大黄’]

names = ['张三', '李四', '大黄', '张三']
names1 = names * 1
names2 = []
for n in names:
	for m in names1:
		if n == m and n not in names2:
			names2.append(n)
names = names2
print(names)

7.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)

scores = [9, 6, 7, 8, 5, 9, 9, 4, 6]
max = min = scores[0]
n = 0
for m in range(len(scores)):
    if max < scores[m]:
    	max = scores[m]
    elif min > scores[m]:
    	min = scores[m]
    n += scores[m]
print('最后总分:', (n - max - min) / (len(scores)))

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 range(len(A)):
    for j in range(len(B)):
        if A[i] == B[j] and n not in C:
            C.append(B[j])
print(C)

9.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)

例如: nums = [19, 89, 90, 600, 1] —> 600

nums = [19, 89, 90, 600, 1]
max = 0
for i in range(len(nums)):
    if max < nums[i]:
        max = nums[i]
print(max)

10.获取列表中出现次数最多的元素

例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3

nums = [1, 2, 3, 1, 4, 2, 1, 3, 7, 3, 3]
n = 0
m = 0
x = 0
for i in nums:
	n = nums.count(i)
	if n > m:
		m = n
		x = i
print(i)
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页