V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Marain
V2EX  ›  Python

大佬们我最近学习筛选就是筛不出来,是哪出问题了

  •  
  •   Marain · 82 天前 · 892 次点击
    这是一个创建于 82 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位大佬我最近学习筛选,有个场景是: 读取列表第四列为新列表 1 , 再在列表 1 每一个字典里面的第四个字典,提取出来为一个新列表 2 , 再把这个新列表 2 里面的第六和第十个字典提取出来,到列表 3 然后筛选出第四个字典所有 key ,到新列表 4 然后求和出每个键的键值和 for line in open(input_file,encoding='utf-8'): if "result" in line: result_file_list.append(['content', 'time1']) continue

    line_tag = line.strip().split('\t')
    main_url = line_tag[3]
    main_url2 = main_url.strip().split('\t')
    main_url3 = main_url2.split('/')[3]
    
    ti = line.main_url3().split('/')
    content = ti.split('/')[5]
    time1 = ti.split('/')[9]
    
    result_file_list.append([content, time1])
    

    with open(input_file, encoding='utf-8') as file: tag_list = [line.strip().split('\t')[2] for line in file if "result" not in line]

    l1 = lin22(tag_list)

    for info in l1: count_result_list.append([info, get_count(info, tag_list), str(round(get_count(info, tag_list) / len(tag_list) * 100, 2)) + '%', str(round(count_result_dict[info] / 3600, 3)) + "(小时)"]) print(info, get_count(info, tag_list), str(round(get_count(info, tag_list) / len(tag_list) * 100, 2)) + '%', str(round(count_result_dict[info] / 3600, 3)) + "(小时)")

    TimG
        1
    TimG  
       82 天前 via Android
    可以用 filter 方法解耦来 debug 。不建议写太长的列表生成式,会给后来人和未来的自己造成很多麻烦。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2889 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:35 · PVG 08:35 · LAX 17:35 · JFK 20:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.