Python經典面試題與參考答案集錦

一些面試題
1.列表的遍歷問題(0507)
list_1 = [10, 20, 30, 40, 50] # 不能對一個列表同時進行 遍歷 和 增刪元素 for element in list_1: print(element) if element == 30 or element == 40: list_1.remove(element) print(list_1)
# 改進 temp_list = list() # 先遍歷列表,記錄下需要增刪的元素 for element in list_1: print(element) if element == 30 or element == 40: temp_list.append(element) # 遍歷完列表,再對需要增刪的內容進行逐一處理 for element in temp_list: list_1.remove(element) print(list_1)
2.字典排序 (0530)
# 題目1 # 按照score的值進行排序。 list_a = [ {"name": "p1", "score": 100}, {"name": "p2", "score": 10}, {"name": "p3", "score": 30}, {"name": "p4", "score": 40}, {"name": "p5", "score": 60}, ] print(sorted(list_a, key=lambda a: a["score"], reverse=False))
# 題目2 # 一行代碼 通過filter 和 lambda 函數輸出以下列表索引為奇數的對應的元素 list_b = [12, 232, 22, 2, 2, 3, 22, 22, 32]
方法一:
new_list=[x[1] for x in fliter(lambdy x:x[0]%2==1,enumerate(list_b))]
方法二:
for index, element in enumerate(list_b): print("") if index % 2 == 0 else print(element)
3.給定三個整數 a, b, c 求和 ,求和之前需要刪除里面相同的整數
示例:
loneSum(1, 2, 3) -> 6
loneSum(3, 2, 3) -> 2
loneSum(3, 3, 3) -> 0
loneSum1 = (1, 2, 3) loneSum2 = (3, 2, 3) loneSum3 = (3, 3, 3) counter = int() for element in loneSum1: if loneSum1.count(element) == 1: counter += element print(counter)
4.給出一個字符串,找出頭尾的鏡像字符串,即從頭正序,從尾倒敘相同的字符串;
示例:
mirrorEnds("abXYZba") -> "ab"
mirrorEnds("abca") -> "a"
mirrorEnds("aba") -> "aba"
def main(source_str): reverse_str = source_str[::-1] for i in range(len(source_str)): if reverse_str[:(i+1)] != source_str[:(i+1)]: return source_str[:i] if reverse_str == source_str: return source_str if __name__ == '__main__': print(main(mirrorEnds))
5.給定一個整數系列 N1, N2…;以及整數M, M是整數系列中的最大數; 從整數系列中取三個數, 可以重復取,要求三個數的和為M。 求所有的可能結果, 并剔除具有相同的結果,得到排序不同的結果。
示例:數列N: [1, 3, 5, 7, 9, 11, 13, 15], 和 M 為17, 返回結果是
[1, 1, 15], [1, 3, 13], [1, 5, 11], [1, 7, 9], [3, 3, 11], [3, 5, 9], [3, 7, 7], [5, 5, 7]
num_list = [1, 3, 5, 7, 9, 11, 13, 15] M = 17 for i in range(len(num_list)): for j in range(i, len(num_list)): for k in range(j, len(num_list)): if num_list[i] + num_list[j] + num_list[k] == M: print((num_list[i], num_list[j], num_list[k]))
6.MRO繼承的問題
求三次輸出的 Parent.x, Child1.x, Child2.x 分別是多少?
lass Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent): pass print(Parent.x, Child1.x, Child2.x) Child1.x = 2 print(Parent.x, Child1.x, Child2.x) Parent.x = 3 print(Parent.x, Child1.x, Child2.x) # 提示:print(Child1.__mro__) #答案:1,1,1 1,2,1 3,2,3
7.參數傳遞的問題
第一題
class Counter(): def __init__(self, count=0): self.count = count def main(): c = Counter() c.name = "zhangsan" times = 0 for i in range(10): increment(c, times) print("count is {0}".format(c.count)) print("times is {0}".format(times)) def increment(c, times): times += 1 c.count += 1 if __name__ == "__main__": main() #答案: # count is 10 # times is 0
第二題
class Count(object): def __init__(self, count=0): self.count = count def main(): c = Count() n = 1 m(c, n) print("count is {0}".format(c.count)) print("n is {0}".format(n)) def m(c, n): c = Count(5) n = 3 if __name__ == '__main__': main() #答案: # count is 0 # n is 1
8.裝飾器/閉包面試題(0615)
def outFun(a): def inFun(x): return a * x return inFun flist1 = [outFun(a) for a in range(3)] for func in flist1: print(func(1)) # fist2 = [lambda x:a*x for a in range(3)] flist2 = [lambda x:a*x for a in range(4)] for func in flist2: print(func(2)) # 答案: 0 1 2, 6 6 6 6
9.字典排序題(zrc)
s = [{1: "a"}, {3: "c"}, {2: "b"}]
請按照字母acsii碼排序
print(sorted(s, key = lambda x: tuple(x.values())[0]))
或
print(sorted(s, key = lambda x: list(x.values())[0]))
sorted 語法:
sorted(iterable[, cmp[, key[, reverse]]])
參數說明:
- iterable -- 可迭代對象。
- cmp -- 比較的函數,這個具有兩個參數,參數的值都是從可迭代對象中取出,此函數必須遵守的規(guī)則為,大于則返回1,小于則返回-1,等于則返回0。
- key -- 主要是用來進行比較的元素,只有一個參數,具體的函數的參數就是取自于可迭代對象中,指定可迭代對象中的一個元素來進行排序。
- reverse -- 排序規(guī)則,reverse = True 降序 , reverse = False 升序(默認)。
返回重新排序的列表。
10.冒泡排序題(zrc)
def sor(): li=[{1:"b"},{3:"c"},{2:"a"}] for i in range(len(li)): for j in range(len(li) - i - 1): v1 = [value for value in list(li[j].values())][0] v2 = [value for value in list(li[j+1].values())][0] print(v1,v2) if v1 > v2: li[j],li[j+1] = li[j+1],li[j] return li ret = sor() print(ret)
11.二分法查找(zrc)
def bin_search(data_set, value): low = 0; high = len(data_set) - 1 while True: mid = (low + high) // 2 if data_set[mid] > value: high = mid - 1 elif data_set[mid] < value: low = mid + 1 else: a = b = mid while data_set[a] == value and a >= 0: a -= 1 while data_set[b] == value and b < len(data_set) - 1: b += 1 return (a+1, b) ret = bin_search([8,8,8,8], 8) print(ret)
12.a=[1,2,3,4,5,6],打印出所有以偶數為key,奇數為值的字典
print({key: value for key, value in zip([i for i in a if i % 2 == 0], [i for i in a if i % 2 != 0])})
13.寫一個函數實現階乘
def factorial(num): counter = 1 for i in range(1, num+1): counter *= i return counter print(factorial(5))
14.有字符串a=‘hsfbsadgasdgvnhhhadhaskdhwqhcjasd’,求出現次數最多的前四個
第一種解法:
def search(a): # 省略校驗邏輯 # 生成包含 element 和 count 數據的集合 data_set = {(element, count) for element, count in zip(a, [a.count(i) for i in a])} print(data_set) # 轉換成列表類型,進行倒序排序 data_list = list(data_set) new_data_list = sorted(data_list, key=lambda x: x[1], reverse=True) # 取出前四名 new_data_list = new_data_list[0:4] # 拼接字符串 return "".join([i[0] for i in new_data_list]) print(search(a))
第二種簡單解法:
a = 'hsfbsadgasdgvnhhhadhaskdhwqhcjasd' # 導入Counter模塊 from collections import Counter # 創(chuàng)建對象 count = Counter(a) # 獲取出現頻率最高的四個字符 print(count.most_common(4)) # 輸出:[('h', 7), ('s', 5), ('a', 5), ('d', 5)]
15.sorted排序
if other_allocated_amount: data_list.append({ "shop_id": 0, "shop_name": "其他", "destination": 2, "current_storage": 0, "demand_amount": 0, "allocated_amount": other_allocated_amount, "priority": 0, "negative_order": 0 }) # 按優(yōu)先級排序 data_list = sorted(data_list, key=lambda d: d["priority"], reverse=True) shop_order = {"倉庫": -999, "總部": 1, "劉園": 2, "侯臺": 3, "咸水沽": 4, "華明": 5, "大港": 6, "楊村": 7, "新立": 8, "大寺": 9, "漢沽": 10, "滄州": 11, "靜海": 13, "蘆臺": 14, "工農村": 15, "唐山": 16, "廊坊": 17, "哈爾濱": 18, "西青道": 19, "雙鴨山": 20, "承德": 21, "張胖子": 22, "固安": 23, "燕郊": 24, "勝芳": 25, "薊縣": 26, } data_list = sorted(data_list, key=lambda d: shop_order.get(d["shop_name"], 999))
相關文章
- 這篇文章主要介紹了兩道阿里python面試題與參考答案,結合具體實例形式分析了Python數組創(chuàng)建、遍歷、拆分及隨機數等相關操作技巧,需要的朋友可以參考下2019-09-02
- 這篇文章主要介紹了60道硬核Python面試題,論面霸是如何煉成的,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-08-28
- 這篇文章主要介紹了關于Python爬蟲面試170道題,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-08-15
- Python是目前編程領域最受歡迎的語言。在本文中,我將總結Python面試中最常見的50個問題。每道題都提供參考答案,感興趣的可以了解下2019-06-26
- 這篇文章主要為大家介紹了Python常見的面試題與相應的Python知識點,包括Python變量、函數、對象、數據類型等,需要的朋友可以參考下2019-06-25
- 這篇文章主要介紹了110道Python面試題,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-24
- 本文給大家分享Python高頻面試題及其答案,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-12-26