温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python有哪些高频面试题

发布时间:2021-11-20 15:20:29 来源:亿速云 阅读:102 作者:iii 栏目:编程语言

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

一. 将字符串 “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有哪些高频面试题”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI