Python测试开发预习课6/18

2024-02-23 07:48
文章标签 python 开发 测试 18 预习

本文主要是介绍Python测试开发预习课6/18,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

注意:
切片是可以越界的
遍历不要改原字符串

1、abcxxx,请统计一下x有多少个?用函数实现

知识点

>>> s="abcaxxx"
>>> s.count("x")
3

在这里插入图片描述
count函数的算法
算法:
1 定义一个函数,参数传递一个字符串
2 声明一个变量letter_count存储某个字符出现的个数
3 遍历字符串,逐一拿出来,判断是否是你想要统计的那个
4 如果是,则letter_count+1
5 如果不是,则什么都不做
6 把函数中的统计结果变量返回回来 return letter_count

def count(s,target_letter):letter_count = 0for i in s:if i == target_letter:letter_count+=1return letter_countprint(count("abcxxx","x"))

在这里插入图片描述

2、abcxabcyabc,请统计一下abc有多少个?用函数实现

算法:
例如:0位置的s[0:3]==“xxx” 当前i是0,
满足的条件下1和2不需要做if判断了,直接跳过去
把当前的i+1,i+2这两个位置,放入到filter_position
代码:

def count(s,target_letters):string_count = 0length=len(target_letters)filter_position = []for i in range(len(s)):if i in filter_position:continueif s[i:i+length] == target_letters:string_count+=1for j in range(1,length):filter_position.append(i+j)return string_countprint(count("xxxxabcxxxx","xxx"))

3、列表的增删改查

>>> arr=[]
>>> type(arr)
<class 'list'>
>>> arr.append(1)
>>> arr.append("1")
>>> arr.append([])
>>> arr.append((1,2))
>>> arr.append(1.24)
>>> arr
[1, '1', [], (1, 2), 1.24]
>>> len(arr)
5
>>> arr.insert(0,"xyz")
>>> arr[0]
'xyz'
>>> arr[2]#列表是一个序列,基于坐标查看
'1'
>>> for i in range(10):
...     arr.append(i)
...
>>> arr
['xyz', 1, '1', [], (1, 2), 1.24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del arr[0]
>>> del arr[0]
>>> arr
['1', [], (1, 2), 1.24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del arr[2:5]
>>> arr
['1', [], 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[0]="aaa"
Traceback (most recent call last):File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> arr[0]="xxx"
>>> arr
['xxx', [], 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> arr[0:4]="111"
>>> arr
['1', '1', '1', 3, 4, 5, 6, 7, 8, 9]
>>> for i in arr:
...     print(i)
...
1
1
1
3
4
5
6
7
8
9
>>> for i in range(len(arr)):
...     print(arr[i])
...
1
1
1
3
4
5
6
7
8
9
>>> a=[1,2,3]
>>> arr=["a","b"]
>>> arr.extend(a)
>>> arr
['a', 'b', 1, 2, 3]

在这里插入图片描述
在这里插入图片描述

4、元组的增删改查–元组:它所有子元素的地址,是不能改变的

>>> a=()
>>> type(a)
<class 'tuple'>
>>> a=(1)
>>> type(a)
<class 'int'>
>>> a=(1,)
>>> type(a)
<class 'tuple'>
>>> a=(1,"a",[],{})
>>> a[0]="x"
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> del a[0]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> a[0]
1
>>> a[1]
'a'
>>> a[2]
[]
>>> a[3]
{}
>>> a[4]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>>
>>>
>>>
>>> a
(1, 'a', [], {})
>>> a[2]
[]
>>> a[2].append(100)
>>> a[2].append(233)
>>> a
(1, 'a', [100, 233], {})
>>> a
(1, 'a', [100, 233], {})

在这里插入图片描述

5、字典的增删改查–字典的key不能重复,如果赋值重复了,会把value替换掉

>>> d={}
>>> type(d)
<class 'dict'>
>>> d["1"]=100
>>> d
{'1': 100}
>>> d[[1]]=100
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> d
{'1': 100}
>>> d[1]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
KeyError: 1
>>> d["1"]
100
>>> d[10000]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
KeyError: 10000
>>> d["2"]=9000
>>> d["2"]=9000
>>> d
{'1': 100, '2': 9000}
>>> del d["2"]
>>> d
{'1': 100}
>>>
>>>
>>>
>>>
>>>
>>> d={1:2,3:4,5:6}
>>> d
{1: 2, 3: 4, 5: 6}
>>> d.keys()
dict_keys([1, 3, 5])
>>> list(d.keys())
[1, 3, 5]
>>> for i in d.keys():
...     print(i)
...
1
3
5
>>> for i in d.values():
...     print(i)
...
2
4
6
>>> for k,v in d.items():
...     print(k,"=",value)
...
Traceback (most recent call last):File "<stdin>", line 2, in <module>
NameError: name 'value' is not defined
>>> for k,v in d.items():
...     print(k,"=",v)
...
1 = 2
3 = 4
5 = 6
>>> d
{1: 2, 3: 4, 5: 6}
>>> d.clear()
>>> d
{}
>>> d={1:2,3:4,5:6}
>>> for i in d.keys():
...     d[i]=1190
...
>>> d
{1: 1190, 3: 1190, 5: 1190}
>>> new_d={}
>>> for i in d.keys():
...     if i%2==1:
...         continue
...         new_d[i]=d[i]
...
>>> new_d[i]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
KeyError: 5
>>> new_d
{}

在这里插入图片描述

这篇关于Python测试开发预习课6/18的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/737965

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数