网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Python有哪些高频面试题

本篇内容主要讲解“Python有哪些高频面试题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python有哪些高频面试题”吧!

创新互联主要从事成都网站制作、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务陆港,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

一. 将字符串 “k:1 |k1:2|k2:3|k3:4”,处理成字典 {k:1,k1:2,

str1 = "k:1|k1:2|k2:3|k3:4"
def str2dict(str1):
 dict1 = {}
 for iterms in str1.split('|'):
 key,value = iterms.split(':')
 dict1[key] = value
 return dict1
#字典推导式
d = {k:int(v) for t in str1.split("|") for k, v in (t.split(":"), )}

二. 请按alist中元素的age由大到小排序

alist = [{'name':'a','age':20},{'name':'b','age':30},{'name':'c','age':25}]
def sort_by_age(list1):
 return sorted(alist,key=lambda x:x['age'],reverse=True)

三. 下面代码的输出结果将是什么?

list = ['a','b','c','d','e']
print(list[10:])

代码将输出[],不会产生IndexError错误,就像所期望的那样,尝试用超出成员的个数的index来获取某个列表的成员。例如,尝试获取list[10]和之后的成员,会导致IndexError。然而,尝试获取列表的切片,开始的index超过了成员个数不会产生IndexError,而是仅仅返回一个空列表。这成为特别让人恶心的疑难杂症,因为运行的时候没有错误产生,导致Bug很难被追踪到。

四. 写一个列表生成式,产生一个公差为11的等差数列

print([x*11 for x in range(10)])

五. 给定两个列表,怎么找出他们相同的元素和不同的元素?

list1 = [1,2,3]
list2 = [3,4,5]
set1 = set(list1)
set2 = set(list2)
print(set1 & set2)
print(set1 ^ set2)

六. 请写出一段python代码实现删除list里面的重复元素?

l1 = ['b','c','d','c','a','a']
l2 = list(set(l1))
print(l2)

用list类的sort方法:

l1 = ['b','c','d','c','a','a']
l2 = list(set(l1))
l2.sort(key=l1.index)
print(l2)

也可以这样写:

l1 = ['b','c','d','c','a','a']
l2 = sorted(set(l1),key=l1.index)
print(l2)

也可以用遍历:

l1 = ['b','c','d','c','a','a']
l2 = []
for i in l1:
 if not i in l2:
 l2.append(i)
print(l2)

七. 给定两个list A,B ,请用找出A,B中相同与不同的元素

A,B 中相同元素: print(set(A)&set(B))
A,B 中不同元素: print(set(A)^set(B))

八. python新式类和经典类的区别?

a. 在python里凡是继承了object的类,都是新式类

b. Python3里只有新式类

c. Python2里面继承object的是新式类,没有写父类的是经典类

d. 经典类目前在Python里基本没有应用

九. python中内置的数据结构有几种?

a. 整型 int、 长整型 long、浮点型 float、 复数 complex

b. 字符串 str、 列表 list、 元祖 tuple

c. 字典 dict 、 集合 set

d. Python3 中没有 long,只有无限精度的 int

十. python如何实现单例模式?请写出两种实现方式?

第一种方法:使用装饰器

def singleton(cls):
 instances = {}
 def wrapper(*args, **kwargs):
 if cls not in instances:
 instances[cls] = cls(*args, **kwargs)
 return instances[cls]
 return wrapper
 
 
@singleton
class Foo(object):
 pass
foo1 = Foo()
foo2 = Foo()
print(foo1 is foo2) # True

第二种方法:使用基类

New 是真正创建实例对象的方法,所以重写基类的new 方法,以此保证创建对象的时候只生成一个实例

class Singleton(object):
 def __new__(cls, *args, **kwargs):
 if not hasattr(cls, '_instance'):
 cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
 return cls._instance
 
 
class Foo(Singleton):
 pass
foo1 = Foo()
foo2 = Foo()
print(foo1 is foo2) # True

第三种方法:元类,元类是用于创建类对象的类,类对象创建实例对象时一定要调用call方法,因此在调用call时候保证始终只创建一个实例即可,type是python的元类

class Singleton(type):
 def __call__(cls, *args, **kwargs):
 if not hasattr(cls, '_instance'):
 cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
 return cls._instance
# Python2
class Foo(object):
 __metaclass__ = Singleton
# Python3
class Foo(metaclass=Singleton):
 pass
foo1 = Foo()
foo2 = Foo()
print(foo1 is foo2) # True

到此,相信大家对“Python有哪些高频面试题”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


本文名称:Python有哪些高频面试题
链接URL:http://bjjierui.cn/article/piehgj.html

其他资讯