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}
有效字母异位词判断
本文介绍了一种使用Python字典来判断两个字符串是否为有效的字母异位词的方法。通过统计每个字符出现的次数并比较两个字符串的字符频次是否一致来实现。此外,文章还介绍了Python字典get()函数的用法。
3033

被折叠的 条评论
为什么被折叠?



