找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

随手科技有限公司随手财富笔试题(金融测试岗)

[复制链接]
本帖最后由 icestick8586 于 2018-8-17 17:01 编辑

1、一个txt文件中已知数据为:
C4D
C4D/maya
C4D
C4D/su
C4D/max/AE

统计每个字段出现的次数,比如C4D,maya,请用最熟悉的语言或者伪代码实现该需求

解决方案一、
  1. def count_test(file_path):
  2.     list1 = []
  3.     with open(file_path, 'r') as f:
  4.     for line in f.readlines():
  5.         result = line.strip().split('/')
  6.         list1.extend(result)
  7.     result = {}
  8.     for i in list1:
  9.         if i in result:
  10.             result[i]=result[i] + 1
  11.         else:
  12.             result[i]=1
  13.     return result
  14. r = count_test("d:\\test.txt")
  15. print(r)
复制代码


解决方案一优化
  1. def count_test(file_path):
  2.     result = {}
  3.     with open(file_path, 'r') as f:
  4.         list1 = [line.strip().split('/') for line in f.readlines()]
  5.     for i in  list1:
  6.         for j in i:
  7.             result[j] = result[j] + 1 if j in  result else  1
  8.     return result
  9. r = count_test("d:\\test.txt")
  10. print(r)
复制代码



分享至 : QQ空间
收藏

1 个回复

倒序浏览
钱老大无敌
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册