看下一个单词

2024-05-26 16:28:15 发布

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

我想知道怎样才能找到下一个字母大写的单词

例如:

ID        Testo

141      Vivo in una piccola città
22       Gli Stati Uniti sono una grande nazione
153      Il Regno Unito ha votato per uscire dall'Europa
64       Hugh Laurie ha interpretato Dr. House 
12       Mi piace bere birra.

我的预期产出是:

ID        Testo                                                 Estratte

141      Vivo in una piccola città                              []
22       Gli Stati Uniti sono una grande nazione                [Gli Stati, Stati Uniti]
153      Il Regno Unito ha votato per uscire dall'Europa        [Il Regno, Regno Unito]
64       Hugh Laurie ha interpretato Dr. House                  [Hugh Laurie, Dr House]
12       Mi piace bere birra.                                   []

要提取大写字母,我需要:

df['Estratte'] = df['Testo'].str.findall(r'\b([A-Z][a-z]*)\b')

但是,此列只收集单个单词,因为代码不会查看下一个单词。 你能告诉我下一个单词我应该加什么条件吗


Tags: id单词ilhousehadrunastati
3条回答
import re
import pandas as pd

x = {141 : 'Vivo in una piccola città',  22: 'Gli Stati Uniti sono una grande nazione', 
      153 : 'Il Regno Unito ha votato per uscire dall\'Europa',  64 : 'Hugh Laurie ha interpretato Dr. House',  12 :'Mi piace bere birra.'}

df = pd.DataFrame(x.items(), columns = ['id', 'testo'])

caps = []
vals = df.testo

for string in vals:
    string = string.split(' ')
    string = string[1:]
    string = ' '.join(string)
    caps.append(re.findall('([A-Z][a-z]+)', string))

df['Estratte'] = caps```

也许你可以用我下面的代码

def getCapitalize(myStr):
    words = myStr.split()
    for i in range(0, len(words) - 1):
        if (words[i][0].isupper() and words[i+1][0].isupper()):
            yield f"{words[i]} {words[i+1]}"

此函数将创建一个生成器,您必须转换为列表或wtv

有时候正则表达式并不总是好的,让我们试试splitexplode

s=df.Testo.str.split(' ').explode()
s2=s.groupby(level=0).shift(-1)
assign=(s + ' ' + s2)[s.str.istitle() & s2.str.isttimeitle()].groupby(level=0).agg(list)
Out[244]: 
1    [Gli Stati, Stati Uniti]
2     [Il Regno, Regno Unito]
3    [Hugh Laurie, Dr. House]
Name: Testo, dtype: object
df['New']=assign
# notice after assign the not find row will be assign as NaN

相关问题 更多 >

    热门问题