我打开了一个文件,并使用正则表达式't'
readlines()
和split()
来删除TAB,它导致了以下列表:
["1", "cats", "--,"]
["2", "chase", "--,"]
["3", "dogs", "--,"]
["1", "the", "--,"]
["2", "car", "--,"]
["3", "is", "--,"]
["4", "gray", "--,"]
现在我想通过循环索引 [0] 上的整数作为句子边界来提取并切成子列表,例如"猫追狗"和"汽车是灰色的"。例如,1 - 3 子列表"猫追狗",然后继续计数 1 - 4 子列表"汽车是灰色的",依此类推,以便我得到子列表["the", "car", "is", "gray" ]
。我该怎么做?
我已经试过了,但出现错误:
无法连接 int + str
将 for 循环中的"i"检测为字符串元素而不是整数:
with open(buffer, 'r') as f:
words = []
for line in f:
items = line.split('t')[:1]
for i in items:
while i>1:
i = i+1
print i
像这样:
from itertools import groupby
with open('yourfile') as fin:
# split lines
lines = (line.split() for line in fin)
# group by consecutive ints
grouped = groupby(enumerate(lines), lambda (idx, el): idx - int(el[0]))
# build sentences from words in groups
sentences = [' '.join(el[1][1] for el in g) for k, g in grouped]
# ['cats chase dogs', 'the car is gray']
注意:这基于您的示例数据:
example = [
["1", "cats", "--,"],
["2", "chase", "--,"],
["3", "dogs", "--,"],
["1", "the", "--,"],
["2", "car", "--,"],
["3", "is", "--,"],
["4", "gray", "--,"]
]
选择合适的数据结构使工作更容易:
container = [["1", "cats", "--,"],
["2", "chase", "--,"],
["3", "dogs", "--,"],
["1", "the", "--,"],
["2", "car", "--,"],
["3", "is", "--,"],
["4", "gray", "--,"]]
将列表嵌套在容器列表中,然后使用字典存储输出列表:
from collections import defaultdict
out = defaultdict(list) # Initialize dictionary for output
key = 0 # Initialize key
for idx, word, _ in container: # Unpack sublists
if int(idx) == 1: # Check if we are at start of new sentence
key += 1 # Increment key for new sentence
out[key].append(word) # Add word to list
给:
{
1: ['cats', 'chase', 'dogs'],
2: ['the', 'car', 'is', 'gray']
}