Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于列表中的多个字符串,如何查找以大写字母开头的字符串中的所有单词_Python_Regex_String_Findall_Capitalization - Fatal编程技术网

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

Python 对于列表中的多个字符串,如何查找以大写字母开头的字符串中的所有单词,python,regex,string,findall,capitalization,Python,Regex,String,Findall,Capitalization,我有一个字符串列表,每个字符串大约有10个句子。我希望从每个字符串中找到以大写字母开头的所有单词。最好在句子的第一个单词之后。我用re.findall来做这个。当我手动设置string=时,这样做没有问题,但是当我尝试使用for循环来循环列表中的每个条目时,我会得到不同的输出 for i in list_3: string = i test = re.findall(r"(\b[A-Z][a-z]*\b)", string) print(test) 输出:

我有一个字符串列表,每个字符串大约有10个句子。我希望从每个字符串中找到以大写字母开头的所有单词。最好在句子的第一个单词之后。我用re.findall来做这个。当我手动设置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']
['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']
当我手动输入字符串值时

txt = 0
for i in list_3:
    string = list_3[txt]
    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']
['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项的输出。有什么想法吗?

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

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

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

您希望将句子作为列表的一个元素,因此下面是解决方案。我们迭代列表3,然后使用split函数迭代每个单词。然后我们检查这个词是否大写。如果是,则将其添加到输出中


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

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

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

您希望将句子作为列表的一个元素,因此下面是解决方案。我们迭代列表3,然后使用split函数迭代每个单词。然后我们检查这个词是否大写。如果是,则将其添加到输出中


据我所知,你们有如下清单:

list_3 = [
  'First sentence. Another Sentence',
  'And yet one another. Sentence',
]
您正在对列表进行迭代,但每次迭代都会覆盖测试变量,因此结果不正确。您必须在附加变量中累积结果,或在每次迭代中立即打印:

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

至于regexp,它忽略了句子中的第一个单词,您可以使用

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

据我所知,你们有如下清单:

list_3 = [
  'First sentence. Another Sentence',
  'And yet one another. Sentence',
]
您正在对列表进行迭代,但每次迭代都会覆盖测试变量,因此结果不正确。您必须在附加变量中累积结果,或在每次迭代中立即打印:

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

至于regexp,它忽略了句子中的第一个单词,您可以使用

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

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

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

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
如果句子可以用一个或两个空格分隔,请插入否定的lookback?之后


如果使用PyPI regex模块,可以使用可变长度lookbehind?

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

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

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
如果句子可以用一个或两个空格分隔,请插入否定的lookback?之后


如果使用PyPI regex模块,可以使用可变长度lookbehind?

由于我非常喜欢regex,请尝试以下方法:

#!/bin/python3
import re

PATTERN = re.compile(r'[A-Z][A-Za-z0-9]*')

all_sentences = [
    "My House! is small",
    "Does Annie like Cats???"
]

def flat_list(sentences):
    for sentence in sentences:
        yield from PATTERN.findall(sentence)

upper_words = list(flat_list(all_sentences))
print(upper_words)

# Result: ['My', 'House', 'Does', 'Annie', 'Cats']

由于我非常喜欢正则表达式,请尝试以下一种:

#!/bin/python3
import re

PATTERN = re.compile(r'[A-Z][A-Za-z0-9]*')

all_sentences = [
    "My House! is small",
    "Does Annie like Cats???"
]

def flat_list(sentences):
    for sentence in sentences:
        yield from PATTERN.findall(sentence)

upper_words = list(flat_list(all_sentences))
print(upper_words)

# Result: ['My', 'House', 'Does', 'Annie', 'Cats']

如果x[0].isupper,则只能执行此操作当我尝试运行此代码时,字符串的全部内容都被打印出来了,而不是打印包含大写字母的单词。我需要附加I.upper吗?@AndrewLittle1你能给我看一下输入吗?我以为你给它的是一个单词。输入的是一串句子,像这样还记得你的大学申请过程吗?单调乏味的普通应用程序、一小时又一小时的研究、ACT/SAT、FAFSA、参观学校等。你还记得是谁帮助你完成了这个过程吗?你的家人和指导顾问,也许你的同龄人,或者你可能没有得到什么帮助。列表_3中的每个元素都是一个包含多个句子的字符串,列表中有五个元素。非常感谢,这是正确的答案,非常感谢您的帮助。如果x[0],我可以这样做。IsUpper当我尝试运行此代码时,打印的不是包含大写字母的单词,而是字符串的全部内容。我需要附加I.upper吗?@AndrewLittle1你能给我看一下输入吗?我以为你给它的是一个单词。输入的是一串句子,像这样还记得你的大学申请过程吗?单调乏味的普通应用程序、一小时又一小时的研究、ACT/SAT、FAFSA、参观学校等。你还记得是谁帮助你完成了这个过程吗?你的家人和指导顾问,也许你的同龄人,或者你可能没有得到什么帮助。列表_3中的每个元素都是一个包含多个se的字符串
句子,列表中有五个元素。非常感谢您,这是正确的答案,我感谢您的帮助。请注意,您不仅匹配大写单词,而且匹配它们前面的空格,当然,这些空格可以轻松删除。请注意,您不仅匹配大写单词,当然,它们前面的空间也很容易被移除。