作业一:
1. 基础题
-
已知一个数字列表,统计列表中十位数是
1
的数的个数list = [123, 23, 119, 34, 19, 213] count = 0 for x in list: if x % 100 // 10 == 1: count += 1
-
已知一个数字列表,将列表中所有元素乘以2
例如: nums = [10, 3, 6, 12] 乘2后: nums = [20, 6, 12, 24]
nums = [10, 3, 6, 12] new_nums = [x * 2 for x in nums] print(new_nums) # [20, 6, 12, 24]
-
已知一个列表,获取列表的中心元素
例如: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[len(nums) // 2 - 1], '和', nums[len(nums) // 2], sep='') else: print('中心元素为:', nums[len(nums) // 2 - 1], sep='') # 中心元素为: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] new_list1 = [] for x in list1: if type(x) == int: new_list1.append(x) print(new_list1) # [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] new_scores = [x for x in scores if x >= 60]
-
已知一个列表保存了多个学生的姓名,要求去掉列表中重复的名字
例如:names = [‘小明’, ‘张三’, ‘李四’, ‘张三’, ‘张三’, ‘小明’, ‘王五’, ‘王五’]
去重后:names = [‘小明’, ‘张三’, ‘李四’, ‘王五’]
names = ['小明', '张三', '李四', '张三', '张三', '小明', '王五', '王五'] new_names = [] for name in names: if name not in new_names: new_names.append(name) print(new_names) # ['小明', '张三', '李四', '王五']
-
已知一个数字列表,获取列表中值最大的元素 (不能使用max函数)
nums = [110, 99, 123, 67, 86, 82, 96, 45] max_num = nums[0] for x in nums[1:]: if x > max_num: max_num = x print(max_num)
-
已知两个有序列表(列表中的元素已经按照从小到大的方式排好序),要求合并两个列表,合并后元素还是从小到大排序
例如: 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] for x in list2: for i in range(len(list1)): if x < list1[i]: list1.insert(i, x) break print(list1)
-
已知一个有序数字列表(从小到大),输入任意一个数字,将输入的数字插入列表中,要求插入后列表仍然保持从小到大排序的关系
例如: 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)):
if num < list1[i]:
list1.insert(i, num)
else:
list1.append(num)
break
print(list1) # 请输入任意一个数字:99 [10, 23, 45, 67, 91, 99]
作业二:
创建一个列表,列表中有10个数字, 保证列表中元素的顺序,对列表进行排重,并对列表使用进行降序排序
例如:[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
--- 去重之后 [70, 88, 91, 107, 234, 177, 282, 197]
---- 降序排序 [282, 234, 197, 177, 107, 91, 88, 70]
nums = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
new_nums = []
for x in nums:
if x not in new_nums:
new_nums.append(x)
print(new_nums) # [70, 88, 91, 107, 234, 177, 282, 197]
new_nums.sort(reverse=True)
print(new_nums) # [282, 234, 197, 177, 107, 91, 88, 70]
- 利用列表推导式, 完成以下需求
a. 生成一个存放1-100中各位数为3的数据列表
结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
list = [x for x in range(3, 94) if x % 10 == 3]
print(list) # [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
b. 利用列表推到是将 列表中的整数提取出来
例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
list1 = [True, 17, "hello", "bye", 98, 34, 21]
new_list = [x for x in list1 if type(x) == int]
print(new_list) # [17, 98, 34, 21]
c.利用列表推导式 存放指定列表中字符串的长度
例如: ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
list = ["good", "nice", "see you", "bye"]
new_list = [len(x) for x in list]
print(new_list) # [4, 4, 7, 3]
d. 利用列表推导式删除列表中整数个位数小于5的元素
例如:[24, 'abc', 99, True, 21, 38, 'hello'] --- ['abc', 99, True, 38, 'hello']
list = [24, 'abc', 99, True, 21, 38, 'hello']
new_list = [x for x in list if type(x) != int or(type(x) == int and x % 10 >= 5)]
print(new_list) # ['abc', 99, True, 38, 'hello']
e. 利用列表推导式获取元素是元组的列表中每个元组的最后一个元素
例如:[(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)] --- [30, 'hello', 3.4, False]
list = [(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]
new_list = [x[-1] for x in list]
print(new_list) # [30, 'hello', 3.4, False]
f.利用列表推导式将数字列表中所有的奇数乘以2,所有的偶数除以2
例如: [23, 4, 67, 88, 90, 21] -> [46, 2, 134, 44, 45, 42]
list = [23, 4, 67, 88, 90, 21]
new_list1 = [x // 2 for x in list if x % 2 == 0]
new_list2 = [x * 2 for x in list if x % 2 != 0]
new_list = new_list1 + new_list2
print(new_list) # [2, 44, 45, 46, 134, 42]
-
已知一个列表获取列表中指定元素所有的下标
例如:[10, 20, 34, 10, 9, 78] 10的下标:[0, 3] 20的下标:[1] 30的下标:[]
num = int(input('输入需要获取下标的元素:')) num_index = [] list = [10, 20, 34, 10, 9, 78] for i in range(len(list)): if list[i] == num: num_index.append(i) print(num, '的下标:', num_index,sep='') # 10的下标:[0, 3] 20的下标:[1] 30的下标:[]
-
*已知一个数字列表,写程序判断这个列表时候是连续递增列表。
例如: [1, 2, 3, 4, 5] -> True [23, 45, 78, 90] -> True [1, 3, 2, 4, 5] -> False
list = [1, 3, 2, 4, 5] for i in range(1, len(list)): if list[i - 1] > list[i]: print(False) break else: if i == len(list) - 1: print(True) # False
-
已知两个列表,将两个列表按照下面的规律交叉合并
A = [10, 20, 30, 40, 50] B = [100, 200, 300] 结果:[10, 100, 20, 200, 30, 300, 40, 50]
A = [10, 20, 30, 40, 50] B = [100, 200, 300] C = [] for i in range(len(A)): for j in range(len(B)): if i == j: C.append(A[i]) C.append(B[j]) for x in A: if x not in C: C.append(x) print(C) # [10, 100, 20, 200, 30, 300, 40, 50]
-
已知两个有序列表,将两个列表合并,合并后的新列表中元素仍然是递增列表
A = [10, 20, 30, 40, 50] B = [25, 44, 60] 结果:[10, 20, 25, 30, 40, 45, 50, 60]
for x in B: for i in range(len(A)): if A[i] > x: A.insert(i, x) break else: if i == len(A)-1: A.append(x) print(A) # [10, 20, 25, 30, 40, 44, 50, 60]