1

假设我有一个带有一些注释的文本文件,然后是这样的键和值:

# The following is
# a list of words and their positions
I: 1
like: 2
to: 3, 5
go: 4
cafes: 6

我将如何把它变成一个句子('我喜欢去咖啡馆')?我想我应该首先尝试将文本转换为字典,但已经无法删除注释并将其拆分为键和值......任何帮助都会很棒!

4

2 回答 2

4

读取文件,将单词和位置作为元组附加到列表中。然后对该列表进行排序,删除索引并加入单词:

with open(inputfilename) as inputfile:
    words = []
    for line in inputfile:
        line = line.strip()
        if not line or line.startswith('#'):
            continue
        word, positions = line.split(':')
        words.extend((int(p), word) for p in positions.split(','))

print ' '.join([w for p, w in sorted(words)])

演示:

>>> with open(inputfilename) as inputfile:
...     words = []
...     for line in inputfile:
...         line = line.strip()
...         if not line or line.startswith('#'):
...             continue
...         word, positions = line.split(':')
...         words.extend((int(p), word) for p in positions.split(','))
... 
>>> print ' '.join([w for p, w in sorted(words)])
I like to go to cafes
于 2013-08-16T13:29:55.630 回答
0

第 1 步将内容读入字典

第 2 步 sorted 对 dict 中的内容进行排序。

shep3 然后最终使用 foreach 获取值,然后通过 + 连接以生成句子。

于 2013-08-16T13:32:21.123 回答