上传文件到网站,网站内容规划怎么写,企业名录搜索软件推荐,品牌策划的意义1. 如何找出整数数组中第二大的数 要点 定义一个函数用于在整数数组里找出第二大的数。 若数组元素少于 2 个#xff0c;则返回 None。 借助两个变量 first 和 second 来跟踪最大数和第二大数。 可以添加异常处理#xff0c;以应对输入非整数数组的情况。 若数组包含重复…1. 如何找出整数数组中第二大的数 要点 定义一个函数用于在整数数组里找出第二大的数。 若数组元素少于 2 个则返回 None。 借助两个变量 first 和 second 来跟踪最大数和第二大数。 可以添加异常处理以应对输入非整数数组的情况。 若数组包含重复元素可考虑返回去重后第二大的数。 示例
python
def find_second_largest(arr):if len(arr) 2:return Nonefirst second float(-inf)for num in arr:if num first:second firstfirst numelif num second and num ! first:second numreturn second if second ! float(-inf) else None# 测试
arr [12, 35, 1, 10, 34, 1]
print(find_second_largest(arr)) 2. 写出如下代码的输出结果并分析
要点 代码中的 multi 函数返回一个包含 4 个 lambda 函数的列表。 这些 lambda 函数形成闭包捕获外部变量 i。 当调用这些 lambda 函数时i 的值为其最终值 3所以输出为 [9, 9, 9, 9]。 若要实现预期的 [0, 3, 6, 9] 输出可使用默认参数来固定 i 的值修改为 [lambda x, ii: i*x for i in range(4)]。
示例
python
def multi():return [lambda x : i*x for i in range(4)]
print([m(3) for m in multi()])3. 统计字符串中字符出现的次数
要点 利用字典来统计字符串中每个字符的出现次数。 遍历字符串若字符已在字典中则计数加 1否则将其添加到字典并初始化为 1。 可使用 collections.Counter 类来简化代码Counter(s) 即可实现相同功能。 统计时可忽略大小写将字符串统一转换为大写或小写后再进行统计。
示例
python
def count_characters(s):char_count {}for char in s:if char in char_count:char_count[char] 1else:char_count[char] 1return char_count# 测试
s hello world
print(count_characters(s)) 4. super 函数的用法和场景 要点 super 函数用于调用父类的方法。 在单继承中可在子类的 __init__ 方法里调用父类的 __init__ 方法实现代码复用。 在多继承中能确保按正确的方法解析顺序MRO调用父类方法。
示例
python
class Parent:def __init__(self):print(Parent __init__)class Child(Parent):def __init__(self):super().__init__()print(Child __init__)c Child()5. 类方法、类实例方法、静态方法的区别 要点 实例方法第一个参数是 self代表实例对象通过实例对象调用用于操作实例的属性和方法。 类方法使用 classmethod 装饰器第一个参数是 cls代表类本身可通过类名或实例对象调用常用于创建工厂方法。 静态方法使用 staticmethod 装饰器无默认第一个参数可通过类名或实例对象调用用于组织代码与类和实例无直接关系。
示例
python
class MyClass:def instance_method(self):return Instance method called, selfclassmethoddef class_method(cls):return Class method called, clsstaticmethoddef static_method():return Static method calledobj MyClass()
print(obj.instance_method())
print(MyClass.class_method())
print(MyClass.static_method())6. 遍历对象的所有属性
要点 使用 dir 函数获取对象的所有属性名。 通过过滤掉以 __ 开头的内置属性打印出用户自定义的属性名。 若要获取属性的值可结合 getattr 函数。 可将属性名和属性值以字典形式存储方便后续处理。
示例
python
class MyClass:def __init__(self):self.name Johnself.age 30obj MyClass()
for attr in dir(obj):if not attr.startswith(__):print(attr)7. 如何定义支持多种操作符的类 要点 通过定义特殊方法使类支持加法、减法、乘法等操作符。 对于不同类型的操作数可进行类型判断并执行相应操作。 可以添加更多操作符支持如除法、取模等。 可以考虑实现反向操作符如 __radd__ 等以支持不同顺序的操作。
示例
python
class MyNumber:def __init__(self, value):self.value value# 加法def __add__(self, other):if isinstance(other, MyNumber):return MyNumber(self.value other.value)return MyNumber(self.value other)# 减法def __sub__(self, other):if isinstance(other, MyNumber):return MyNumber(self.value - other.value)return MyNumber(self.value - other)# 乘法def __mul__(self, other):if isinstance(other, MyNumber):return MyNumber(self.value * other.value)return MyNumber(self.value * other)# 字符串表示def __str__(self):return str(self.value)# 测试
a MyNumber(5)
b MyNumber(3)
print(a b)
print(a - b)
print(a * b) 8. 比较Cython、Pypy、CPython、Numba 的缺点 要点 Cython学习成本高需掌握特定语法代码可移植性受影响调试复杂。 Pypy与 CPython 兼容性有问题启动时间长内存占用高。 CPython执行速度慢尤其是 CPU 密集型任务存在全局解释器锁GIL限制多线程并行性能。 Numba支持的 Python 语法和数据类型有限需提前确定输入数据类型编译时间可能较长。 9. 抽象类和接口类的区别和联系
要点 联系都不能实例化用于定义规范和约束子类行为都可包含抽象方法要求子类实现。 区别抽象类可包含抽象方法、具体方法和属性接口类通常只含抽象方法无具体实现和属性。 10. 如何动态获取和设置对象的属性
要点 使用 getattr 函数动态获取对象的属性。 使用 setattr 函数动态设置对象的属性。 可结合 hasattr 函数先检查属性是否存在再进行获取或设置操作。
示例
python
class MyClass:def __init__(self):self.name Johnobj MyClass()
attr_value getattr(obj, name)
print(attr_value) class MyClass2:passobj2 MyClass2()
setattr(obj2, age, 30)
print(obj2.age) 文章转载自: http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.zwyuan.com.gov.cn.zwyuan.com http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.mzwqt.cn.gov.cn.mzwqt.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.yrbq.cn.gov.cn.yrbq.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.lmzpk.cn.gov.cn.lmzpk.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.fllfz.cn.gov.cn.fllfz.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.kybpj.cn.gov.cn.kybpj.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn