Python-[--001--]-Python涉及的演算法面試題
阿新 • • 發佈:2020-08-05
第一題:將 a = [1,1,1,2,2,3,3,3,4,5,6,6,6,7,8,8]轉化為c = [1,1,2,2,3,3,4,5,6,6,7,8]
分析:對出現相同元素次數大於2的進行處理
a = [1,1,1,2,2,3,3,3,4,5,6,6,6,7,8,8] # c = [1,1,2,2,3,3,4,5,6,6,7,8] b = [] for i in a: if b.count(i) < 2: b.append(i) print(b)
第二題:找出字串中重複的內容
f = "asklabassk"
def find_distinct(string): l1=[] # 去重的 l2 =[] # 重複的 for i in range(0, len(string)): if string[i] in l1: l2.append(string[i]) else: l1.append(string[i]) return set(l2) f = "asklabassk" res = find_distinct(f) print(res)
--------------------------
{'s', 'a', 'k'}
第三題:統計字串中元素出現的次數
def count_each_char_1(string): res = {} for i in string: if i not in res: res[i] = 1 else: res[i] += 1 return res print(count_each_char_1('aenabsascd'))
------------------------------
{'a': 3, 'e': 1, 'n': 1, 'b': 1, 's': 2, 'c': 1, 'd': 1}