1. 基础题
-
已知一个数字列表,打印列表中所有的奇数。
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for i in nums: if i % 2 != 0: print(i)
-
已知一个数字列表,打印列表中所有能被3整除但是不能被2整除的数。
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for i in nums: if i % 3 == 0 and i % 2 != 0: print(i)
-
已知一个数字列表,计算所有偶数的和。
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum = 0 for i in nums: if i % 2 == 0: sum += i print(sum)
-
已知一个数字列表,统计列表中十位数是
1
的数的个数。nums = [10, 33, 9, 59, 19, 44, 34, 17, 18, 130, 319] count = 0 for i in nums: if i // 10 % 10 == 1: count += 1 print(count) 结果: 5
-
已知一个列表,获取列表中下标为奇数的所有元素(从0开始的下标值)。
#方法一 list1 = [10, 20, 5, 34, 90, 8] list = [] for i in range(len(list1)): if i % 2 != 0: list.append(list1[i]) print(list) #方法二:切片 print(list1[1::2]) 结果:[20, 34, 8]
-
已知一个数字列表,将列表中所有元素乘以2。
例如: nums = [10, 3, 6, 12] 乘2后: nums = [20, 6, 12, 24]
#方法一:直接创建新列表 nums = [10, 3, 6, 12] num = [] for i in nums: i *= 2 num.append(i) print(num) #方法二:修改原列表 for i, item in enumerate(nums): nums[i] = item * 2 print(nums)
-
已知一个列表,获取列表的中心元素。
例如:nums = [10, 2, 6, 12] -> 中心元素为: 2和6
nums = [10, 2, 6, 12, 10] -> 中心元素为:6
nums = [10, 2, 6, 12] if len(nums) % 2 == 0: print(nums[int(len(nums) / 2) - 1]) print(nums[int(len(nums) / 2)]) else: print(nums[int(len(nums) / 2)]) #简化版:将重复使用的存入一个变量中 index = len(nums) // 2 if len(nums) % 2 == 0: print(nums[index - 1]) print(nums[index]) else: print(nums[index]) 结果: 2 6
-
已知一个列表,获取列表中所有的整型元素。
例如:list1 = [10, 1.23, ‘abc’, True, 100, ‘hello’, ‘20’, 5]
结果是: [10, 100, 5]
list1 = [10, 1.23, 'abc', True, 100, 'hello', '20', 5] list = [] for i in list1: if type(i) == int: list.append(i) print(list) 结果: [10, 100, 5]
2. 进阶题
-
定义一个列表保存多个学生的分数,删除列表中所有低于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] #方法一 for i in range(len(scores) - 1, -1, -1): if scores[i] < 60: #remove scores.remove(scores[i]) #pop scores.pop(i) #del del scores[i] print(scores) #方法二 # 复制一份(3种方法)不能new_scores = scores直接相等 new_scores = scores[:] # new_scores = scores*1 # new_scores = scores+[] for x in new_scores: if x < 60: scores.remove(x) print(scores)
错误写法:
for i in scores: if i < 60: scores.remove(i) print(scores) """ 执行过程: 获取元素的时候没有将原列表中的所有元素都获取到。 scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] 第1次:i=45 -> if 45<60 -> if True:scores.remove(45) -> scores = [ 60, 89, 30, 12, 59, 99, 80, 71, 66] 第2次:i=89 -> if 89<60 -> if False 第3次:i=30 -> if 30<60 -> if True:scores.remove(30) -> scores = [ 60, 89, 12, 59, 99, 80, 71, 66] 第4次:i=59 -> if 59<60 -> if True:scores.remove(59) -> scores = [ 60, 89, 12, 99, 80, 71, 66] 第5次:i=80 -> if 80<60 -> if False 第6次:i=71 -> if 71<60 -> if False 第7次:i=66 -> if 66<60 -> if True:scores.remove(66) -> scores = [ 60, 89, 12, 99, 80, 71] 第8次:循环结束 """
-
已知一个列表保存了多个学生的姓名,要求去掉列表中重复的名字。
例如:names = [‘小明’, ‘张三’, ‘李四’, ‘张三’, ‘张三’, ‘小明’, ‘王五’, ‘王五’]
去重后:names = [‘小明’, ‘张三’, ‘李四’, ‘王五’]
names = ['小明', '张三', '李四', '张三', '张三', '小明', '王五', '王五'] #方法一 name=[] for i in names: if i not in name: name.append(i) print(name) #方法二 for i in range(len(names) - 1, -1, -1): if names.count(names[i]) > 1: del names[i] print(names) #方法三 count = len(names) for x in range(count): name = names.pop() if name not in names: names.insert(0, name) print(names)
-
已知一个数字列表,获取列表中值最大的元素 (不能使用max函数)。
nums = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] #方法一 for i in range(len(nums) - 1): if nums[i] > nums[i + 1]: nums[i], nums[i + 1] = nums[i + 1], nums[i] print(nums[-1]) #方法二 max_val = nums[0] for i in nums: if i > max_val: max_val = i print(max_val) 结果:99
-
已知两个有序列表(列表中的元素已经按照从小到大的方式排好序),要求合并两个列表,合并后元素还是从小到大排序。
例如: 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 = list1 + list2 for i in range(len(list3) - 1): for j in range(len(list3) - 1): if list3[j] > list3[j + 1]: list3[j], list3[j + 1] = list3[j + 1], list3[j] print(list3) #方法二 list3 = list1 + list2 list3.sort() print(list3) #方法三:效率更高 list3 = [] while True: a = list1.pop(0) b = list2.pop(0) if a < b: list3.append(a) list2.insert(0, b) else: list3.append(b) list1.insert(0,a) if list1 == [] or list2 == []: break list3 += list1 + list2 print(list3)
-
已知一个有序数字列表(从小到大),输入任意一个数字,将输入的数字插入列表中,要求插入后列表仍然保持从小到大排序的关系。
例如: list1 = [10, 23, 45, 67, 91] 输入: 50 -> list1 = [10, 23, 45, 50, 67, 91]
list1 = [10, 23, 45, 67, 91] num = int(input('请输入一个数:')) for i in range(len(list1) - 1): if list1[i] < num < list1[i + 1]: list1.insert(i + 1, num) elif num > list1[-1]: list1.append(num) elif num < list1[0]: list1.insert(0, num) print(list1) #优化: for index, item in enumerate(list1): if item > num: list1.insert(index, num) break else: list1.append(num) print(list1)