九个Python列表生成式高频面试题汇总
作者:赵卓不凡 时间:2023-06-04 20:09:51
1. 引言
之前已经有博客专门介绍了Python中的列表生成式,可能大家还不太擅长。这里推荐九个Python列表生成式的面试题(从简单到困难排序),可以帮助大家提高列表生成式的理解水平。
闲话少说,我们直接开始吧!
2. 字符串转整数
假设我们有以下字符串列表:
lis = ["1", "2", "3", "4", "5"]
我们希望用代码实现列表生成式用以将列表中的每个字符串转化为相应的整数,这里假设每一个列表中的元素均可以转化为整数,即样例的输出如下:
[1, 2, 3, 4, 5]
【参考答案】
lis = ["1", "2", "3", "4", "5"]
answer = [int(i) for i in lis]
print(answer)
3. 大于10的数字
假设我们有以下整数列表:
lis = [1,5,13,4,16,7]
我们希望用代码实现列表生成式用以找出上述列表中大于10的数字。即样例的输出如下:
[13, 16]
【参考答案】
lis = [1,5,13,4,16,7]
answer = [i for i in lis if i>10]
print(answer)
4. 大于10且整除3的数字
假设我们有以下整数列表:
lis = [1,12,13,14,15,2,3]
我们希望用代码实现列表生成式用以找出上述列表中大于10且可以整除3的数字。即样例的输出如下:
[12, 15]
【参考答案】
lis = [1,12,13,14,15,2,3]
answer = [i for i in lis if i>10 and i%3==0]
print(answer)
5. 对列表中的偶数执行加1操作
假设我们有以下整数列表:
lis = [1,2,4,5,7]
我们希望用代码实现列表生成式用以对上述列表中的偶数执行加1的操作。即样例的输出如下:
[1,3,5,5,7]
提示: 可以使用三目运算符
【参考答案】
lis = [1,2,4,5,7]
answer = [(i+1 if i%2==0 else i) for i in lis]
print(answer)
6. 包含数字1的数字
实现一个列表生成式,用以求出在数字1到100之间所有包含数字1的数字。
即样例的输出如下:
[1,10,11,12,13,14,15,16,17,18,19,21,31,41,51,61,71,81,91,100]
【参考答案】
answer = [i for i in range(1,101) if "1" in str(i)]
print(answer)
7. 合并两个列表
假设我们有两个长度相同的列表,分别为fruits和prices , 两个列表中的元素分别如下:
fruits = ["apple", "orange", "pear"]
prices = [4,5,6]
我们希望用代码实现列表生成式用以对上述两个列表进行合并,即列表中的元素为(fruit, price)形式。即样例的输出如下:
[("apple",4), ("orange",5), ("pear",6)]
提示: 推荐使用zip函数
【参考答案】
fruits = ["apple", "orange", "pear"]
prices = [4,5,6]
answer = [(fruit, price) for fruit, price in zip(fruits, prices)]
print(answer)
8. 根据value对字典排序
假设我们有以下字典,key为水果名称fruits,value为相应的单价price,如下:
d = {"apple":5, "orange":2, "pear":7, "durian":6}
我们需要实现列表生成式用以对上述字典按price对相应的元素进行排序。即样例输出如下:
[("orange",2), ("apple",5), ("durian",6), ("pear",7)]
提示: 推荐使用 sorted函数
【参考答案】
d = {"apple":5, "orange":2, "pear":7, "durian":6}
answer = sorted([(f,p) for f,p in d.items()], key=lambda x:x[-1])
print(answer)
9. 求两个列表的元素组合
假设我们有以下两个列表:
fruits = ["apple", "orange", "pear"]
recipes = ["pie", "juice"]
我们需要实现列表生成式用以对上述两个列表中的元素求解组合操作。即样例输出如下:
[
("apple", "pie"), ("apple", "juice"),
("orange", "pie"), ("orange", "juice"),
("pear", "pie"), ("pear", "juice")
]
提示: 使用两层嵌套循环
【参考答案】
fruits = ["apple", "orange", "pear"]
recipes = ["pie", "juice"]
answer = [(f,r) for f in fruits for r in recipes]
print(answer)
10. 列表中两个元素的唯一组合,其和为3的倍数
假设我们有以下整数列表:
lis = [1,6,2,4,7]
我们希望用列表生成式来实现在此列表中查找任意两个元素的唯一组合,其总和为3的倍数。即上述样例预期输出为:
[(1,2), (2,4), (2,7)]
【参考答案】
lis = [1,6,2,4,7]
answer = [(i,j) for i in lis for j in lis if i<j and (i+j)%3==0]
print(answer)
11. 总结
本文重点介绍了九个Python列表生成式的面试题,并给出了相应的代码实现,可以加深大家对列表生成式的理解。
来源:https://blog.csdn.net/sgzqc/article/details/124935374
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Vue组件化开发思考
![](https://img.aspxhome.com/file/2023/3/133153_0s.png)
Pytorch中实现只导入部分模型参数的方式
python 制作本地应用搜索工具
![](https://img.aspxhome.com/file/2023/6/117466_0s.png)
uniapp中微信小程序与H5相互跳转以及传参详解(webview)
![](https://img.aspxhome.com/file/2023/9/136699_0s.png)
详解Python中的type()方法的使用
解析python 中/ 和 % 和 //(地板除)
MySQL性能优化技巧分享
python虚拟环境完美部署教程
python求crc32值的方法
VMware中linux环境下oracle安装图文教程(一)
![](https://img.aspxhome.com/file/2023/6/65656_0s.jpg)
flash(swf)遮住网页内容div的解决
python中面向对象的注意点概述总结
![](https://img.aspxhome.com/file/2023/6/81516_0s.jpg)
Python从数据库读取大量数据批量写入文件的方法
win2000server IIS和tomcat5多站点配置
Django中login_required装饰器的深入介绍
PHP实现动态删除XML数据的方法示例
![](https://img.aspxhome.com/file/2023/1/122421_0s.png)
Python生成器实现简单"生产者消费者"模型代码实例
![](https://img.aspxhome.com/file/2023/7/107947_0s.png)
python利用json和pyecharts画折线图实例代码
![](https://img.aspxhome.com/file/2023/1/71441_0s.jpg)
Pytorch 如何实现常用正则化
![](https://img.aspxhome.com/file/2023/1/133511_0s.png)