1. 基础题
-
已知一个数字列表,打印列表中所有的奇数
list1 = [10, 9, 6, 13, 45, 22] result = [x for x in list1 if x % 2 != 0] print(result) # [9, 13, 45]
-
已知一个数字列表,打印列表中所有能被能被3整除但是不能被2整除的数
list1 = [10, 9, 6, 13, 45, 22, 12, 33] list2 = [] for x in list1: if x % 3 == 0 and x % 2 != 0: list2.append(x) print(list2)
-
已知一个数字列表,计算所有偶数的和
list1 = [10, 9, 6, 13, 45, 22, 12, 33] list2 = [] for x in list1: if x % 2 == 0: list2.append(x) print(sum(list2)) # 50
-
已知一个数字列表,统计列表中十位数是
1
的数的个数list1 = [11, 9, 61, 13, 45, 21, 12, 33] count = 0 for x in list1: if x % 10 == 1: count += 1 print(count) # 3
-
已知一个列表,获取列表中下标为奇数是所有元素(从0开始的下标值)
list1 = [10, 20, 5, 34, 90, 8] list2 = [] for index in range(len(list1)): if index % 2 != 0: list2.append(list1[index]) print(list2) # [20, 34, 8]
例如: list1 = [10, 20, 5, 34, 90, 8]
结果:[20, 34, 8]
-
已知一个数字列表,将列表中所有元素乘以2
nums = [10, 3, 6, 12] nums = [x * 2 for x in nums] print(nums) # [20, 6, 12, 24]
例如: nums = [10, 3, 6, 12] 乘2后: nums = [20, 6, 12, 24]
-
已知一个列表,获取列表的中心元素
nums = [10, 2, 6, 12] nums1 = [] n = len(nums)//2 if len(nums) % 2 == 0: for index in range(len(nums)): if index == n or index == n - 1: print(index) nums1.append(nums[index]) else: for index in range(len(nums)): if index == n: nums1.append(nums[index]) print(nums1)
例如:nums = [10, 2, 6, 12] -> 中心元素为: 2和6
nums = [10, 2, 6, 12, 10] -> 中心元素为:6
-
已知一个列表,获取列表中所有的整型元素
list1 = [10, 1.23, 'abc', True, 100, 'hello', '20', 5] list2 = [] for x in list1: if type(x) == int: list2.append(x) print(list2) # [10, 100, 5]
例如:list1 = [10, 1.23, ‘abc’, True, 100, ‘hello’, ‘20’, 5]
结果是: [10, 100, 5]
2. 进阶题
-
定义一个列表保存多个学生的分数,删除列表中所以低于60分的值
# 方法1: scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] scores1 = [] for x in scores: if x >= 60: scores1.append(x) print(scores1) # [60, 89, 99, 80, 71, 66] # 方法2:对原列表进行备份,遍历备份数据,删除数据 scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] new_scores = scores * 1 for x in new_scores: if x < 60: scores.remove(x) print(scores) # 方法3 # 解决方案2:通过下标遍历 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] 删除后: scores = [60, 89, 99, 80, 71, 66]
-
已知一个列表保存了多个学生的姓名,要求去掉列表中重复的名字
# 方法1 names = ['小明', '张三', '李四', '张三', '张三', '小明', '王五', '王五'] names1 = [] for x in names: if x not in names1: names1.append(x) print(names1) # ['小明', '张三', '李四', '王五'] # 方法2 for x in range(len(names)): n = names.pop(0) if n not in names: names.append(n) print(names) # ['李四', '张三', '小明', '王五']
例如:names = [‘小明’, ‘张三’, ‘李四’, ‘张三’, ‘张三’, ‘小明’, ‘王五’, ‘王五’]
去重后:names = [‘小明’, ‘张三’, ‘李四’, ‘王五’]
-
已知一个数字列表,获取列表中值最大的元素 (不能使用max函数)
nums = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] max1 = nums[0] for x in nums: if x > max1: max1 = x print(max1)
-
已知两个有序列表(列表中的元素已经按照从小到大的方式排好序),要求合并两个列表,合并后元素还是从小到大排序
例如: list1 = [10, 23, 39, 41, 52, 55, 80] list2 = [9, 38, 55, 70]
合并后的结果: [9, 10, 23, 38, 39, 41, 52, 55, 55, 70, 80]
list1 = [10, 23, 39, 41, 52, 55, 80] list2 = [9, 38, 55, 70] list3 = [] while True: a1 = list1.pop(0) b1 = list2.pop(0) if a1 < b1: list3.append(a1) list2.insert(0, b1) else: list3.append(b1) list1.insert(0, a1) if list1 == [] or list2 == []: list3 += list2 + list1 break print(list3)
-
已知一个有序数字列表(从小到大),输入任意一个数字,将输入的数字插入列表中,要求插入后列表仍然保持从小到大排序的关系
例如: list1 = [10, 23, 45, 67, 91] 输入: 50 -> list1 = [10, 23, 45, 50, 67, 91]
list1 = [10, 23, 45, 67, 91] n = int(input('输入:')) for index in range(len(list1)): if list1[index] > n: list1.insert(index, n) break else: list1.append(n) print(list1)