列表练习题

1. 基础题

  1. 已知一个数字列表,打印列表中所有的奇数

    list = [2, 5, 9, 65, 46, 20]
    for i in list:
        if i % 2 != 0:
            print(i, end=' ')
    
  2. 已知一个数字列表,打印列表中所有能被能被3整除但是不能被2整除的数

    list = [2, 5, 6, 9, 65, 46, 20, 21]
    for i in list:
        if (i % 3 == 0) and i % 2 != 0:
            print(i, end=' ')
    
  3. 已知一个数字列表,计算所有偶数的和

    sum1 = 0
    list = [2, 5, 6, 9, 65, 46, 20, 21]
    for i in list:
        if i % 2 == 0:
            sum1 += i
    print(sum1)
    
  4. 已知一个数字列表,统计列表中十位数是1的数的个数

    count = 0
    list = [215, 55, 62, 19, 65, 416, 20, 21]
    for i in list:
        i = i % 100 // 10
        if i == 1:
            count += i
    print(count)
    
  5. 已知一个列表,获取列表中下标为奇数是所有元素(从0开始的下标值)

    例如: list1 = [10, 20, 5, 34, 90, 8]

    结果:[20, 34, 8]

    list1 = [10, 20, 5, 34, 90, 8]
    print(list1[1::2])
    
  6. 已知一个数字列表,将列表中所有元素乘以2

    例如: nums = [10, 3, 6, 12] 乘2后: nums = [20, 6, 12, 24]

    nums = [10, 3, 6, 12]
    nums1 = []
    for i in nums:
        i *= 2
        nums1.append(i)
    print(nums1)
    
  7. 已知一个列表,获取列表的中心元素

    例如:nums = [10, 2, 6, 12] -> 中心元素为: 2和6

    ​ nums = [10, 2, 6, 12, 10] -> 中心元素为:6

    nums1 = [10, 2, 6, 12, 10]
    a = len(nums1)
    if a % 2 == 0:
        print('中心元素为:', nums1[a % 2+1], nums1[a % 2+2])
    else:
        print('中心元素为:', nums1[a % 2+1])
    
  8. 已知一个列表,获取列表中所有的整型元素

    例如:list1 = [10, 1.23, ‘abc’, True, 100, ‘hello’, ‘20’, 5]

    ​ 结果是: [10, 100, 5]

    list1 = [10, 1.23, 'abc', True, 100,  'hello', '20', 5]
    list2 = []
    for i in list1:
        if type(i) == int:
            list2.append(i)
    print(list2)
    

2. 进阶题

  1. 定义一个列表保存多个学生的分数,删除列表中所以低于60分的值

    例如: scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] 删除后: scores = [60, 89, 99, 80, 71, 66]

    # 方法一:
    scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    scores1 = []
    for i in scores:
        if i >= 60:
            scores1.append(i)
    scores = scores1
    print(scores)
    # 方法二:
    scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    for index in range(len(scores)-1, -1, -1):
        if scores[index] < 60:
            del scores[index]
    print(scores)
    # 方法三:
    scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    count = len(scores)
    for index in range(count):
        item = scores.pop()
        if item >= 60:
            scores.insert(0, item)
    print(scores)
    方法四:
    scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    for i in scores[:]:
        if i < 60:
            scores.remove(i)
    print(scores)
    
  2. 已知一个列表保存了多个学生的姓名,要求去掉列表中重复的名字

    例如:names = [‘小明’, ‘张三’, ‘李四’, ‘张三’, ‘张三’, ‘小明’, ‘王五’, ‘王五’]

    ​ 去重后:names = [‘小明’, ‘张三’, ‘李四’, ‘王五’]

    #方法一:
    names = ['小明', '张三', '李四', '张三', '张三', '小明', '王五', '王五']
    names1 = []
    for name in names:
        if name not in names1:
            names1.append(name)
    print(names1)
    
  3. 已知一个数字列表,获取列表中值最大的元素 (不能使用max函数)

    scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    max1 = scores[0]
    for i in scores:
        max1 = i if i>max1 else max1
    print(max1)
    
  4. 已知两个有序列表(列表中的元素已经按照从小到大的方式排好序),要求合并两个列表,合并后元素还是从小到大排序

    例如: list1 = [10, 23, 39, 41, 52, 55, 80] list2 = [9, 38, 55, 70]

    合并后的结果: [9, 10, 23, 38, 39, 41, 52, 55, 55, 70, 80]

    A = [10, 23, 39, 41, 52, 55, 80]
    B = [9, 38,  55, 70]
    C = []
    a = len(A)
    b = len(B)
    x = 0
    y = 0
    while x < a and y < b:
        for i in A:
            for j in B:
                if i < j:
                    C.append(i)
                    A.remove(i)
                    x += 1
                    break
                else:
                    C.append(j)
                    B.remove(j)
                    y += 1
            break
    if x < a:
        print(C+A)
    if y < b:
        print(C+B)
    
  5. 已知一个有序数字列表(从小到大),输入任意一个数字,将输入的数字插入列表中,要求插入后列表仍然保持从小到大排序的关系

    例如: list1 = [10, 23, 45, 67, 91] 输入: 50 -> list1 = [10, 23, 45, 50, 67, 91]

    list1 = [10, 23, 45, 67, 91]
    x = float(input('请输入任意一个数字:'))
    y = 0
    for i in list1:
        if i > x:
            if x % 1 != 0:
                list1.insert(y, x)
            else:
                list1.insert(y, int(x))
            break
        else:
            y += 1
    print(list1)
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值