day5-列表作业 2021/9/20

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

scores = [100, 90, 83, 92, 56, 78, 99, 67, 66, 95, 78]
if len(scores) % 2:
    print(scores[len(scores) // 2])
else:
    print(scores[len(scores) // 2 - 1], scores[len(scores) // 2])

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

scores = [100, 90, 83, 92, 56, 78, 99, 67, 66, 95, 78]
sum_1 = 0
for i in scores:
    sum_1 += i
print(sum_1)

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

scores = [100, 90, 83, 92, 56, 78, 99, 67, 66, 95, 78]
print(scores[1::2])

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

scores = [100, 90, 83, 92, 56, 78, 99, 67, 66, 95, 78]
for i in scores:
    if i % 2:
        print(i, end=' ')

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

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

nums = [1, 2, 3, 4]
nums_new = []
i = 0
while i < len(nums):
    i_new = nums[i] * 2
    i += 1
    nums_new.append(i_new)
    if i == len(nums):
        break
print(nums_new)

6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的

例如:names = [‘张三’, ‘李四’, ‘大黄’, ‘大黄’, ‘张三’, ‘张三’, ‘张三’] -> names = [‘张三’, ‘李四’, ‘大黄’]

names = ['张三', '李四', '大黄', '大黄', '张三', '张三', '张三']
names_new = []
for i in names:
    if i not in names_new:
        names_new.append(i)
    else:
        continue
print(names_new)

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

scores = [100, 90, 83, 92, 56, 78, 99, 67, 66, 95, 78]
scores_1 = scores[:]
sum_1 = 0
for i in scores:
    if i == max(scores) or i == min(scores):
        scores_1.remove(i)
    else:
        sum_1 += i
print(sum_1 / len(scores_1))

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 not in B and i not in C:
        continue
    else:
        C.append(i)
print(C)

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

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

nums = [19, 89, 90, 600, 1]
i, x = 0, 0
for i in nums:
    if i >= x:
        x = i
print(x)

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]
print(max(nums, key=nums.count))
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值