我在Vim中打开了一个文件:
py24-sqlalchemy
py25-beautifulsoup
py25-beautifulsoup4
py25-bpython
py25-epydoc
py25-icalendar
py25-ipython
py25-libgmail
py25-mechanize
py25-numpy
py25-pil
py25-simplejson
py25-sphinx
py25-sqlalchemy
py25-sqlite
py25-tkinter
py25-vobject
py26-appscript
py26-beautifulsoup
py26-bpython
py26-epydoc
py26-game
py26-icalendar
py26-ipython
py26-mechanize
py26-numpy
py26-pdfminer
py26-simplejson
py26-sphinx
py26-sqlalchemy
py26-sqlite
py26-tkinter
py26-vobject
py27-appscript
py27-asciitable
py27-asciitable
py27-beautifulsoup
py27-beautifulsoup4
py27-blist
py27-bpython
py27-chm
py27-configobj
py27-dateutil
py27-epydoc
py27-game
py27-gdal
py27-gtk
py27-ipython
py27-lxml
py27-matplotlib
py27-mechanize
py27-mysql
py27-numpy
py27-pdfminer
py27-pil
py27-prettytable
py27-progressbar
py27-psutil
py27-py2app
py27-pylint
py27-pyobjc
py27-pyobjc-cocoa
py27-pypdf
py27-pyqt4
py27-simplejson
py27-sphinx
py27-sqlalchemy
py27-sqlite
py27-termcolor
py27-tkinter
py27-tz
py27-wxpython
py31-appscript
py31-asciitable
py31-beautifulsoup4
py31-blist
py31-bpython
py31-game
py31-lxml
py31-psutil
py32-asciitable
py32-beautifulsoup4
py32-blist
py32-bpython
py32-game
py32-ipython
py32-lxml
py32-psutil
# ... etc ... (many lines)我想要过滤行,以便对于2.x范围内的Python版本,我只保留最新的包。
示例:如果文件同时包含py25-ipython和py26-ipython,我希望py25-ipython消失,而py26-ipython保留。总是只保留最新的版本,并且每个包总是只有一个版本。
这是我想出来的,但它的编码很糟糕。在我看来,几乎就像是C64-BASIC代码。
#!/usr/bin/env python2.7
import sys
import re
PATTERN_PYTHON_MACPORT = '^py(2[4567])-(\w[-\w]*)$'
REGEX_PYTHON_MACPORT = re.compile(PATTERN_PYTHON_MACPORT)
def main():
packages = {}
filtered_lines = []
for line in sys.stdin:
match = REGEX_PYTHON_MACPORT.match(line)
if match:
python_version = int(match.group(1))
package_name = match.group(2)
if package_name in packages:
packages[package_name].append(python_version)
else:
packages[package_name] = [python_version]
else:
filtered_lines.append(line)
for package_name in packages:
versions = packages[package_name]
if len(versions) == 1:
version_to_keep = versions[0]
else:
version_to_keep = sorted(versions, reverse=True)[0]
filtered_lines.append('py{}-{}\n'.format(version_to_keep,
package_name))
for line in sorted(filtered_lines):
sys.stdout.write(line)
if __name__ == '__main__':
main()我怎么才能让它更具Pythonic风格呢?在Vimscript中这样做会更容易吗?我可能更喜欢Vimscript中的解决方案。
顺便说一下,我是Python和Vimscript的初学者。在这里学习。代码示例会很棒。
PS我使用:'<,'>! /Users/tinosino/Desktop/pyfilter.py从Vim内部运行过滤器
发布于 2013-02-22 07:12:06
这将做同样的事情(假设您的输入列表已经排序)。否则,请使用sorted(sys.stdin)。如果你不想保留Python 3.x模块,那就去掉'else‘子句。
import re
rex = re.compile('py2\d+-(\S+)')
modules = {}
for line in sys.stdin:
match = rex.match(line)
if match:
modules[match.group(1)] = match.group(0)
else:
modules[line] = line
print '\n'.join(sorted(modules.values()))输出:
py25-libgmail
py26-icalendar
py26-vobject
py27-appscript
py27-asciitable
py27-beautifulsoup
py27-beautifulsoup4
py27-blist
py27-bpython
py27-chm
py27-configobj
py27-dateutil
py27-epydoc
py27-game
py27-gdal
py27-gtk
py27-ipython
py27-lxml
py27-matplotlib
py27-mechanize
py27-mysql
py27-numpy
py27-pdfminer
py27-pil
py27-prettytable
py27-progressbar
py27-psutil
py27-py2app
py27-pylint
py27-pyobjc
py27-pyobjc-cocoa
py27-pypdf
py27-pyqt4
py27-simplejson
py27-sphinx
py27-sqlalchemy
py27-sqlite
py27-termcolor
py27-tkinter
py27-tz
py27-wxpython
py31-appscript
py31-asciitable
py31-beautifulsoup4
py31-blist
py31-bpython
py31-game
py31-lxml
py31-psutil
py32-asciitable
py32-beautifulsoup4
py32-blist
py32-bpython
py32-game
py32-ipython
py32-lxml
py32-psutilhttps://stackoverflow.com/questions/15013935
复制相似问题