我在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
答案 0 :(得分:1)
这将做同样的事情(假设您已经订购了输入列表)。否则,请使用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-psutil