溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python有哪些高頻面試題

發(fā)布時(shí)間:2021-11-20 15:20:29 來(lái)源:億速云 閱讀:96 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“Python有哪些高頻面試題”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“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ǎo)式
d = {k:int(v) for t in str1.split("|") for k, v in (t.split(":"), )}

二. 請(qǐng)按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)

三. 下面代碼的輸出結(jié)果將是什么?

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

代碼將輸出[],不會(huì)產(chǎn)生IndexError錯(cuò)誤,就像所期望的那樣,嘗試用超出成員的個(gè)數(shù)的index來(lái)獲取某個(gè)列表的成員。例如,嘗試獲取list[10]和之后的成員,會(huì)導(dǎo)致IndexError。然而,嘗試獲取列表的切片,開(kāi)始的index超過(guò)了成員個(gè)數(shù)不會(huì)產(chǎn)生IndexError,而是僅僅返回一個(gè)空列表。這成為特別讓人惡心的疑難雜癥,因?yàn)檫\(yùn)行的時(shí)候沒(méi)有錯(cuò)誤產(chǎn)生,導(dǎo)致Bug很難被追蹤到。

四. 寫一個(gè)列表生成式,產(chǎn)生一個(gè)公差為11的等差數(shù)列

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

五. 給定兩個(gè)列表,怎么找出他們相同的元素和不同的元素?

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

六. 請(qǐng)寫出一段python代碼實(shí)現(xiàn)刪除list里面的重復(fù)元素?

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)

七. 給定兩個(gè)list A,B ,請(qǐng)用找出A,B中相同與不同的元素

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

八. python新式類和經(jīng)典類的區(qū)別?

a. 在python里凡是繼承了object的類,都是新式類

b. Python3里只有新式類

c. Python2里面繼承object的是新式類,沒(méi)有寫父類的是經(jīng)典類

d. 經(jīng)典類目前在Python里基本沒(méi)有應(yīng)用

九. python中內(nèi)置的數(shù)據(jù)結(jié)構(gòu)有幾種?

a. 整型 int、 長(zhǎng)整型 long、浮點(diǎn)型 float、 復(fù)數(shù) complex

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

c. 字典 dict 、 集合 set

d. Python3 中沒(méi)有 long,只有無(wú)限精度的 int

十. python如何實(shí)現(xiàn)單例模式?請(qǐng)寫出兩種實(shí)現(xiàn)方式?

第一種方法:使用裝飾器

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 是真正創(chuàng)建實(shí)例對(duì)象的方法,所以重寫基類的new 方法,以此保證創(chuàng)建對(duì)象的時(shí)候只生成一個(gè)實(shí)例

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

第三種方法:元類,元類是用于創(chuàng)建類對(duì)象的類,類對(duì)象創(chuàng)建實(shí)例對(duì)象時(shí)一定要調(diào)用call方法,因此在調(diào)用call時(shí)候保證始終只創(chuàng)建一個(gè)實(shí)例即可,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

到此,相信大家對(duì)“Python有哪些高頻面試題”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI