当前位置: 首页 > 知识库问答 >
问题:

如何为列表中的多个字符串查找以大写字母开头的字符串中的所有单词

姜运珧
2023-03-14

我有一个字符串列表,每个字符串大约有10个句子。我希望从每个字符串中找到以大写字母开头的所有单词。优选地在句子中的第一个词之后。我正在使用re。我要做这件事。当我手动设置string=''时,我不会遇到任何问题,但是当我尝试使用for循环来循环列表中的每个条目时,我会得到不同的输出。

for i in list_3:
    string = i
    test = re.findall(r"(\b[A-Z][a-z]*\b)", string)
print(test)

输出:

['I', 'I', 'As', 'I', 'University', 'Illinois', 'It', 'To', 'It', 'I', 'One', 'Manu', 'I', 'I', 'Once', 'And', 'Through', 'I', 'I', 'Most', 'Its', 'The', 'I', 'That', 'I', 'I', 'I', 'I', 'I', 'I']

当我手动输入字符串值时

txt = 0
for i in list_3:
    string = list_3[txt]
    test = re.findall(r"(\b[A-Z][a-z]*\b)", string)
print(test)

输出:

['Remember', 'The', 'Common', 'App', 'Do', 'Your', 'Often', 'We', 'Monica', 'Lannom', 'Co', 'Founder', 'Campus', 'Ventures', 'One', 'Break', 'Campus', 'Ventures', 'Universities', 'Undermatching', 'Stanford', 'Yale', 'Undermatching', 'What', 'A', 'Yale', 'Lannom', 'There', 'During', 'Some', 'The', 'Lannom', 'That', 'It', 'Lannom', 'Institutions', 'University', 'Chicago', 'Boston', 'College', 'These', 'Students', 'If', 'Lannom', 'Recruiting', 'Elite', 'Campus', 'Ventures', 'Understanding', 'Campus', 'Ventures', 'The', 'For', 'Lannom', 'What', 'I', 'Wish', 'I', 'Knew', 'Before', 'Starting', 'Company', 'I', 'Even', 'I', 'Lannom', 'The', 'There']

但我似乎无法编写一个for循环来正确打印列表中5项的输出。有什么想法吗?

共有3个答案

方和顺
2023-03-14

据我所知,你有这样的列表:

list_3 = [
  'First sentence. Another Sentence',
  'And yet one another. Sentence',
]

您正在迭代列表,但每次迭代都会覆盖test变量,因此您的结果不正确。您必须在附加变量中累积结果或立即打印它,每次迭代:

acc = []
for item in list_3:
  acc.extend(re.findall(regexp, item))
print(acc)

或者

for item in list_3:
  print(re.findall(regexp, item))

至于正则表达式,它忽略了句子中的第一个单词,您可以使用

re.findall(r'(?<!\A)(?<!\.)\s+[A-Z]\w+', s) 
  • <代码>(?

你可能会收到以空格为前缀的单词,所以这是最后一个例子:

acc = []
for item in list_3:
  words = [w.strip() for w in re.findall(r'(?<!\A)(?<!\.)\s+[A-Z]\w+', item)]
  acc.extend(words)
print(acc)
邹祺
2023-03-14

假设句子用一个空格分隔,您可以将re.findall与以下正则表达式一起使用。

r'(?m)(?<!^)(?<![.?!] )[A-Z][A-Za-z]*'

启动你的引擎!| Python代码

Python的正则表达式引擎执行以下操作。

(?m)         : set multiline mode so that ^ and $ match the beginning
               and the end of a line
(?<!^)       : negative lookbehind asserts current location is not
               at the beginning of a line
(?<![.?!] )  : negative lookbehind asserts current location is not
               preceded by '.', '?' or '!', followed by a space
[A-Z]        : match an uppercase letter
[A-Za-z]*    : match 1+ letters

如果句子可以用一个或两个空格隔开,则插入否定的look(?

如果使用PyPI正则表达式模块,则可以使用可变长度的look(?

邢灿
2023-03-14

最简单的方法是为编写一个循环,检查列表元素的第一个字母是否大写。如果是,它将附加到输出列表中。

output = []
for i in list_3:
    if i[0] == i[0].upper():
        output.append(i)
print(output)

我们也可以使用列表理解,并在一行中完成。我们还检查元素的第一个字母是否是大写字母。

output = [x for x in list_3 if x[0].upper() == x[0]]
print(output)

编辑

你想把这个句子作为一个列表的元素,所以这里是解决方案。我们迭代< code>list_3,然后使用< code>split()函数迭代每个单词。然后我们检查这个单词是否大写。如果是,它将被添加到< code >输出中。

list_3 = ["Remember your college application process? The tedious Common App applications, hours upon hours of research, ACT/SAT, FAFSA, visiting schools, etc. Do you remember who helped you through this process? Your family and guidance counselors perhaps, maybe your peers or you may have received little to no help"]
output = []
for i in list_3:
    for j in i.split():
        if j[0].isupper():
            output.append(j)
print(output)
 类似资料:
  • 我在大学学习Java,我需要写一个静态int countCapitals(String s)方法,返回字符串s中有多少个单词以大写字母开头。 大写字母是UPPERCASE_LETTER类型或TITLECASE_LETTER类型的字符(Character)。单词是由一个或多个空格、字符、符号或标点符号分隔的字母或数字序列。 New.countCapitals("亲爱的朋友们,你们好!这里—以大写字母

  • 问题内容: 因此,我试图在用户输入的字符串中查找所有大写字母,但始终出现此运行时错误: 我觉得很愚蠢,但我无法弄清楚,Oracle甚至在有关java.lang.StringIndexOutOfBoundsException的页面上谈论了charAt。 这是我的代码,用于查找大写字母并打印它们: 我非常感谢您的任何投入和/或帮助。 问题答案: 应该 请记住,数组索引从零开始。 字符串长度返回 字符串

  • 给定一个字符串s和一个非空字符串p,在s中找到p的字母表的所有起始索引。 null

  • 问题内容: 我正在尝试从Java字符串中找到所有三个字母子字符串。 例如,从字符串“ example string”中,我应该得到“ exa”,“ xam”,“ amp”,“ mpl”,“ ple”,“ str”,“ tri”,“ rin”,“ ing”。 我尝试使用Java正则表达式“([[a-zA-Z]){3}”,但仅得到“ exa”,“ mpl”,“ str”,“ ing”。 有人可以告诉我

  • 问题内容: 我有一个字符串:“ hello good old world”,我想将每个单词的每个首字母大写,而不是使用.toUpperCase()整个字符串。是否有现成的java助手可以完成这项工作? 问题答案: 看看ACL WordUtils。

  • 问题内容: 如何使用MySQL查询来计算大写字母?我现在正在尝试 但这给我一个错误的说法: 我猜,我不允许在AGAINST子句中使用列表,这很烂 那么,有没有办法实现这一目标? 问题答案: 试试这个功能- 例子: