当前位置:网站首页>day6-列表作业
day6-列表作业
2022-08-05 05:13:00 【非鱼丶丶】
1. 基础题
- 已知一个数字列表,打印列表中所有的奇数
num = [1, 2, 3, 4, 5]
for x in num:
if x % 2 != 0:
print(x)
- 已知一个数字列表,打印列表中所有能被能被3整除但是不能被2整除的数
num = [1, 2, 3, 4, 5]
for x in num:
if x % 2 != 0 and x % 3 == 0:
print(x)
- 已知一个数字列表,计算所有偶数的和
num = [1, 2, 3, 4, 5]
sum1 = 0
for x in num:
if x % 2 == 0:
sum1 += x
print(sum1)
- 已知一个数字列表,统计列表中十位数是
1的数的个数
num = [10, 20, 30, 40, 50]
count = 0
for x in num:
if x % 100 // 10 == 1:
count += 1
print(count)
已知一个列表,获取列表中下标为奇数是所有元素(从0开始的下标值)
例如: list1 = [10, 20, 5, 34, 90, 8]
结果:[20, 34, 8]
num = [10, 20, 30, 40, 50]
for index in range(len(num)):
if index % 2 != 0:
print(num[index])
已知一个数字列表,将列表中所有元素乘以2
例如: nums = [10, 3, 6, 12] 乘2后: nums = [20, 6, 12, 24]
num = [10, 20, 30, 40, 50]
nums = []
for x in num:
x *= 2
nums.append(x)
print(nums)
已知一个列表,获取列表的中心元素
例如:nums = [10, 2, 6, 12] -> 中心元素为: 2和6
nums = [10, 2, 6, 12, 10] -> 中心元素为:6
num = [10, 2, 6, 12]
a = len(num)
if a % 2 == 0:
print(num[a // 2 - 1])
print(num[a // 2])
else:
print(num[(a - 1) // 2 ])
已知一个列表,获取列表中所有的整型元素
例如:list1 = [10, 1.23, ‘abc’, True, 100, ‘hello’, ‘20’, 5]
结果是: [10, 100, 5]
num = [10, 1.23, 'abc', True, 100, 'hello', '20', 5]
nums = []
for x in num:
if type(x) == int:
nums.append(x)
print(nums)
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 x in scores[:]:
if x < 60:
scores.remove(x)
print(scores)
已知一个列表保存了多个学生的姓名,要求去掉列表中重复的名字
例如:names = [‘小明’, ‘张三’, ‘李四’, ‘张三’, ‘张三’, ‘小明’, ‘王五’, ‘王五’]
去重后:names = [‘小明’, ‘张三’, ‘李四’, ‘王五’]
names = ['小明', '张三', '李四', '张三', '张三', '小明', '王五', '王五']
res = []
for x in names:
if x not in res:
res.append(x)
print(res)
- 已知一个数字列表,获取列表中值最大的元素 (不能使用max函数)
num = [10, 20, 90, 40, 50]
c = num[0]
for x in num:
if x > c:
c = x
print(c)
已知两个有序列表(列表中的元素已经按照从小到大的方式排好序),要求合并两个列表,合并后元素还是从小到大排序
例如: 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, 45, 67, 91] 输入: 50 -> list1 = [10, 23, 45, 50, 67, 91]
边栏推荐
- After controlling the export file in MySQL, it becomes \N. Is there any solution?
- 类的底层机制
- 将照片形式的纸质公章转化为电子公章(不需要下载ps)
- [cesium] element highlighting
- 【过一下6】机器视觉视频 【过一下2被挤掉了】
- Machine Learning (2) - Machine Learning Fundamentals
- entry point injection
- day11-函数作业
- Transformation 和 Action 常用算子
- Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction
猜你喜欢

开发一套高容错分布式系统

Using QR codes to solve fixed asset management challenges

Flex layout frog game clearance strategy

Lecture 4 Backpropagation Essays

【Transfer】What is etcd

Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction

flex布局青蛙游戏通关攻略

MySQL Foundation (1) - Basic Cognition and Operation

DOM及其应用

数据库实验五 备份与恢复
随机推荐
redis cache clearing strategy
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
The underlying mechanism of the class
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
【过一下4】09-10_经典网络解析
redis 缓存清除策略
In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
【过一下10】sklearn使用记录
【cesium】Load and locate 3D Tileset
Flutter learning 2-dart learning
Mesos学习
【过一下8】全连接神经网络 视频 笔记
Geek卸载工具
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
多线程查询结果,添加List集合
Difference between for..in and for..of
入口点注入
Transformation 和 Action 常用算子
【Untitled】
[Go through 7] Notes from the first section of the fully connected neural network video