day4作业

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

scores = [78, 67, 52, 78, 99, 23]
a = 0
for i in scores:
    a += 1
if a & 1 == 0:
    print(scores[int(a/2 - 1)])
    print(scores[int(a/2)])
else:
    print(scores[int(a/2 - 0.5)])

#方法1
count = len(list1)
if count & 1:
    print(list1[count//2])
else:
    print(list1[count//2])
    print(list1[count//2+1])

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

scores = [78, 67, 52, 78, 99, 23]
x = 0
for i in scores:
    x += i
print(x)

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

scores = [78, 67, 52, 78, 99, 23]
a = -1
for i in scores:
    a += 1
    if a & 1 == 1:
        print(scores[a])
# 方法
for index in range(1, len(list1), 2):
    print(list1[index])

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

scores = [78, 67, 52, 78, 99, 23]
for i in scores: 
    if i & 1 == 1:
        print(i)

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

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

scores = [78, 67, 52, 78, 99, 23]
a = 0
for i in scores:
    scores[a] = 2 * i
    a += 1
print(scores)

#别的方法
for index, item in enumerate(nums):
    nums[index] = item*2

for index in range(len(nums)):
    nums[index] *= 2

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

scores = [1, 1, 1, 2, 2, 2, 3, 3, 5, 2]
scores = list(set(scores))
print(scores)

# 方法1
new_names = []
for name in names:
    if name not in new_names:
        new_names.append(name)
print(new_names)
# 方法2
for name in names[:]:
    for names.count(name)>1:
        names.remove(name)
print(names)

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

scores = [78, 67, 52, 75, 99, 23]
a = 0
for i in scores:
    if a >= i:
        pass
    else:
        a = i
scores.remove(a)
for i in scores:
    if a <= i:
        pass
    else:
        a = i
scores.remove(a)
sum1 = 0
count = 0
for i in scores:
    sum1 += i
    count += 1
print(sum1/count)

# 改
scores.remove(max(scores))
scores.remove(min(scores))
print('最后得分:', sum(scores)/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 A:
    if i in B:
            c.append(i)
print(c)

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

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

scores = [78, 67, 52, 75, 99, 23]
max_score = 0
for score in scores:
    if max_score < score:
        max_score = score
print(score)

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

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

scores = [1, 1, 1, 2, 2, 2, 3, 3, 5, 2]
scores2 = list(set(scores))
scores3 = []
for i in scores2:
    count = 0
    for j in scores:
        if i == j:
            count += 1
    a = count
    scores3.append(a)
b = 0
for i in scores3:
    if b >= i:
        pass
    else:
        b = i
c = scores3.index(b)
print(scores2[c])

# 方法1
max_count = 0
for x in nums:
    count = nums.count(x)
    if count > max_count:
        max_count = count
  
new_num = []
for x in nums:
    if nums.count(x) == max_count and x not in new_num:
        new_num.append(x)
print(new_num)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值