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
ooof
V2EX  ›  Python

一个筛选字符的问题

  •  
  •   ooof ·
    ooof · 2013-02-06 20:13:20 +08:00 · 2736 次点击
    这是一个创建于 4068 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我有这样一个csv 格式的文件,类似如下数据:

    wintonzhang,kobebryant
    wintonzhang,thescript
    wintonzhang,tomhanks
    wintonzhang,VictoriaJustice
    mynte,shek0309
    shek0309,aboluowang
    shek0309,aiww
    shek0309,aoi_sola
    shek0309,AshlynnBrookeX
    shek0309,BarackObama
    mema55159,13super10
    mema55159,18_asooma
    mema55159,59arb
    mema55159,121_remo
    mema55159,123Kyuna
    mema55159,222amam

    我想做的是,如果一个词在全文中只出现过一次,那么就把这个词所在的行删除。

    实际需要处理的数据有十几万行,在 Excel 中有办法处理,但速度实在太慢了。

    不知道还有什么简单易行的方法。

    谢谢!
    11 条回复    1970-01-01 08:00:00 +08:00
    wwwjfy
        1
    wwwjfy  
       2013-02-06 21:18:23 +08:00
    这类处理awk应该比python更合适
    alexrezit
        2
    alexrezit  
       2013-02-06 21:32:07 +08:00
    @wwwjfy
    一打开贴子就看到你这句... 不过... 查了查文档, 咱还是试试吧... 楼主你要的是这个不?
    http://gist.github.com/AlexRezit/4722511
    alexrezit
        3
    alexrezit  
       2013-02-06 21:32:47 +08:00
    alexrezit
        4
    alexrezit  
       2013-02-06 21:32:56 +08:00
    wwwjfy
        5
    wwwjfy  
       2013-02-06 21:55:19 +08:00
    @alexrezit 让人 当伸手党 不是什么好事.. 字符串处理python 肯定不快

    另外,个人感觉写得太复杂了,没必要用re,csv这种简单的文件结构就直接一行读用逗号split就行
    ps, string.join已经deprecated了,推荐'\n'.join
    alexrezit
        6
    alexrezit  
       2013-02-06 22:41:39 +08:00
    @wwwjfy
    我根本不会 Python... =, =
    ooof
        7
    ooof  
    OP
       2013-02-06 23:30:25 +08:00
    @alexrezit 非常感谢,我在试着运行你写的代码!

    我先找少量的数据,看代码的输出正确了,才读那个大文件,然后还不知道会怎么样 ... 大概需要一些时间吧。

    @wwwjfy 真是很悲剧,经常想鼓捣些东西,但是又没有恒心和耐心把代码学的哪怕是基本熟悉一点,经常是要用的时候,才胡子眉毛一把抓 ... 汗 ... 也非常感谢你的补充修改!
    alexrezit
        8
    alexrezit  
       2013-02-07 11:38:11 +08:00
    @ooof
    提醒你一下... 这个会占用很多内存, 因为是把数据全部读取进去再比较. 我不会 Python 瞎写着玩儿的. =v=
    ooof
        9
    ooof  
    OP
       2013-02-07 11:43:46 +08:00
    @alexrezit 恩,已经体会到了:

    十几万基本 Python 就停止下来了;后来换到1000条左右,会出一个结果。

    在 python-cn 讨论组,有一个方法,下面是部分代码,但我还没有运行它:

    counts, linenos = {}, {}
    sd = counts.setdefault # 节省attribute lookup,不知道有没有必要
    for lineno, line in enumerate(lines):
    words = line.split(',') # 不能处理单词内含逗号的情况
    for wd in words:
    if wd in counts:
    # 出现过2次了,无视
    continue
    if wd in linenos:
    # 出现过一次了,剔除
    counts[wd] = None
    del linenos[wd]
    else:
    # 记录行号(这个是0-based)
    linenos[wd] = lineno
    return linenos
    stillzhl
        10
    stillzhl  
       2013-02-07 18:01:48 +08:00 via Android
    可以试试collections.Count,专门用来计数的,在手机上看帖,没办法写代码
    ooof
        11
    ooof  
    OP
       2013-02-07 18:21:20 +08:00
    @stillzhl ,谢谢。

    在 Twitter 数据的收集和处理(3) http://book.51cto.com/art/201203/325328.htm 看到有 collections.Counter 应用的介绍。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1137 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:42 · PVG 02:42 · LAX 11:42 · JFK 14:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.