python dictionary VS collections
【摘要】 测试代码如下:import collectionsimport timeimport osdef get_doc_list_relative_path(subFolder,filetype="txt"): """ :param subFolder: :param filetype: :return:file relative paths as list """ f...
测试代码如下:
import collections import time import os def get_doc_list_relative_path(subFolder,filetype="txt"): """ :param subFolder: :param filetype: :return:file relative paths as list """ file_list = list() for root, dirs, files in os.walk(".\\" + subFolder, topdown=False): for name in files: if name.split(".")[-1] == filetype:#attention! file_list.append(str(os.path.join(root, name))) file_list = sorted(file_list)#Do NOT delete this line!!! return file_list def get_tf_dict(x): res = dict() for t in x: res[t] = res.get(t, 0) + 1 return res if __name__ == '__main__': s = get_doc_list_relative_path("doc") for i in s: print(i) with open(i, "r", encoding='UTF-8') as f: text = f.read() splitedText_list = text.split(' ') # list time_start = 0 time_end = 0 time_start = time.thread_time_ns() s = collections.Counter(splitedText_list) time_end = time.thread_time_ns() print('time cost with collections method:', time_end - time_start, 'ns') time_start = 0 time_end = 0 time_start = time.thread_time_ns() s2 = get_tf_dict(splitedText_list) time_end = time.thread_time_ns() print('time cost with dictionary method.:', time_end - time_start, 'ns') print(s["the"]) print(s2["the"])
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)