列表列表。。名单的数量?应用regex和n

2024-04-25 22:12:29 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我简化我的问题:

我有一个列表(文档),其中包含一些列表(句子),如str。就像a = [['Sent1 from first doc!','Sent2 from first doc.'],['Sent1 from 2nd doc.','Sent2 from 2nd doc.']]

现在我试着把每个句子分成一个单词列表。。因此,我可能会有一个包含一个列表(句子)的第一个列表(文档),其中每个列表都包含一个列表(该句子中的单词作为str)。你知道吗

不幸的是,我的代码生成了一个包含每个单词的(句子)列表。。因此,我不知道每个句子来自哪个文档。你知道吗

我的代码如下所示:

sentcs = []
for i in range(len(a)): 
    for p in range(len(a[i])):        
        spr = re.findall(r'[A-Z]?[^A-Z\s]+|[A-Z]+', a[i][p])
        sentcs.append(spr) 

但那不是我想要的。。我想要一份名单。。或者是这样编程的坏习惯?你知道吗


Tags: infrom文档列表fordoclenrange
1条回答
网友
1楼 · 发布于 2024-04-25 22:12:29
    li = [('Help! Be nice.'),('Thx. Help appreciated.')]

    for el in li:
        l = el.split(' ',1)
        print(tuple((l[0], l[1:])))  

    ('Help!', ['Be nice.'])
    ('Thx.', ['Help appreciated.'])


from nltk.tokenize import sent_tokenize   

st = ['Help! Be nice.','Thx. Help appreciated.']

for el in st:
    t = sent_tokenize(el)
    print(tuple((t[0], t[1:])))

('Help!', ['Be nice.'])
('Thx.', ['Help appreciated.'])

相关问题 更多 >