Hash Table/Set 雜湊表
計數
算有一個字串中每個字元各有幾個:透過字串的索引
s = 'apple'
table = {}
for i in range(len(s)):
if s[i] in table:
table[s[i]] += 1
else:
table[s[i]] = 1
print(table)
# {'a': 1, 'p': 2, 'l': 1, 'e': 1}算有一個字串中每個字元各有幾個:直接遍歷字元
s = 'apple'
table = {}
for char in s:
if char in table:
table[char] += 1
else:
table[char] = 1
print(table)
# {'a': 1, 'p': 2, 'l': 1, 'e': 1}算有一個陣列每個每個字串各有幾個
算有一個陣列中每個每個數字出現幾次
API
功能
Last updated