dict =defaultdict(factory_function)
from collections import defaultdict dict1 = defaultdict(int) # dict1[1]=0 dict2 = defaultdict(set) # dict2[1]=set() dict3 = defaultdict(str) # dict3[1]= dict4 = defaultdict(list) # dict4[1]=[
应用举例: 题目描述:
def isAnagram(s, t): """ :type s: str :type t: str :rtype: bool """ dict_s = {} for item in s: if item not in dict_s.keys(): dict_s[item] = 1 else: dict_s[item] += 1 dict_t = {} for item in t: if item not in dict_t.keys(): dict_t[item] = 1 else: dict_t[item] += 1 return dict_s == dict_t
def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ from collections import defaultdict dict_s = defaultdict(int) dict_t = defaultdict(int) for item in s: dict_s[item] += 1 for item in t: dict_t[item] += 1 return dict_s == dict_t
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理