class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
dict1,dict2 = {},{}
for item in s:
dict1[item] = dict1.get(item,0)+1
for item in t:
dict2[item] = dict2.get(item,0)+1
print(dict1)
return dict1==dict2
Python
字典(Dictionary) get()
函数返回指定键的值,如果值不在字典中返回默认值。
语法
get()
方法语法:
dict.get(key, default=None)
参数
key
– 字典中要查找的键。
default
– 如果指定键的值不存在时,返回该默认值值。
返回值
返回指定键的值,如果值不在字典中返回默认值None
。
- 导入包的方法
from collections import Counter
#以字典的形式返回
d = Counter('wer')
#{'w':1,'e':1,'r':1}