泰迪云课堂大数据培训班分享Python练手题
2023-09-11 14:17:06 时间
泰迪云课堂大数据培训班分享Python练手题,大家有需要的可以边学边做
1. 已知一个字符串为 “hello_world_yoyo”,如何得到一个队列 [“hello”,”world”,”yoyo”] ?
使用 split 函数,分割字符串,并且将数据转换成列表类型:
test = 'hello_world_yoyo'
print(test.split("_"))
12
结果:
['hello', 'world', 'yoyo']
2. 有个列表 [“hello”, “world”, “yoyo”],如何把列表里面的字符串联起来,得到字符串 “hello_world_yoyo”?
使用 join 函数将数据转换成字符串:
test = ["hello", "world", "yoyo"]
print("_".join(test))
结果:
hello_world_yoyo
如果不依赖 python 提供的 join 方法,还可以通过 for 循环,然后将字符串拼接,但是在用“+”连接字符串时,结果会生成新的对象,使用 join 时结果只是将原列表中的元素拼接起来,所以 join 效率比较高。
for 循环拼接如下:
test = ["hello", "world", "yoyo"]
# 定义一个空字符串
j = ''
# 通过 for 循环打印出列表中的数据
for i in test:
j = j + "_" + i
# 因为通过上面的字符串拼接,得到的数据是“_hello_world_yoyo”,前面会多一个下划线_,所以把这个下划线去掉
print(j.lstrip("_"))
3. 把字符串 s 中的每个空格替换成”%20”,输入:s = “We are happy.”,输出:“We%20are%20happy.”。
使用 replace 函数,替换字符换即可:
s = 'We are happy.'
print(s.replace(' ', '%20'))
12
结果:
We%20are%20happy.
4. Python 如何打印 99 乘法表?
for 循环打印:
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(j, i, i*j), end='')
print()
while 循环实现:
i = 1
while i <= 9:
j = 1
while j <= i:
print("%d*%d=%-2d"%(i,j,i*j),end = ' ') # %d: 整数的占位符,'-2'代表靠左对齐,两个占位符
j += 1
print()
i += 1
结果:
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
5. 从下标 0 开始索引,找出单词 “welcome” 在字符串“Hello, welcome to my world.” 中出现的位置,找不到返回 -1。
def test():
message = 'Hello, welcome to my world.'
world = 'welcome'
if world in message:
return message.find(world)
else:
return -1
print(test())
结果:
7
相关文章
- Python 爬虫 性能 相关( asyncio 模块 --- 高性能爬虫 )
- 可视化运行Python的神器Jupyter Notebook
- Python string生成随机数
- 【华为OD机试真题 python】机器人走迷宫 【2022 Q4 | 200分】
- Google Earth Engine(GEE)——Python 气候分析和预测
- 使用python,将mongo数据写入es数据库中
- Python分享:python爬虫可以用来做什么?
- 测试人员为什么学 Python ?能做什么事?
- gyp ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- python中stack在实际中的简单应用之平衡符号
- 《Python数据科学指南》——1.9 生成一个迭代器和生成器
- 《Python数据科学指南》——1.17 使用映射函数
- 《Python数据可视化编程实战》——5.3 创建3D直方图
- 《Python数据可视化编程实战》—— 导读
- python学习之把字典的键值拼接成字符串
- 「基于Python技术的智慧中医商业项目」Django前端主页数据交互
- Python数据可视化 Pyecharts 制作 WordCloud 词云图
- Python数据可视化 Pyecharts 制作 Treemap 矩形树图
- Nvidia GPU 入门教程之 08 如何在 Python 中使用 Kaggle API,直接从 Kaggle 快速下载数据集
- 如何毫不费力地探索您的 IDX 数据集,使用 idx2numpy 库在 Python 中探索原始 MNIST 文件
- (数据科学学习手札30)朴素贝叶斯分类器的原理详解&Python与R实现
- (数据科学学习手札23)决策树分类原理详解&Python与R实现
- 【Python分布式服务框架】python实现gRPC服务
- 10. 使用python构建Kafka的生产者和消费者,将txt转换成csv文件
- Python 生成周期性波动的数据 可指定数值范围