如何检查字符串列表中的字符串中是否有大写字母?

问题描述 投票:0回答:1

我有一个单词列表:

str_list = [“There”, “is”, “ a Red”, “”, “shirt, but not a white One”]

我想检查列表中的每个单词是否都有大写字母,如果是,我想创建一个这样的新列表:

split_str_list = [“There”, “is”, “ a ”, ”Red”, “”, “shirt, but a white “, ”One”]

我试过了:

for word in range(len(str_list)):
if word.isupper():
    print str_list[word]

但它没有检查每个字母,而是检查字符串中的所有字母。

python string list
1个回答
4
投票

你可以使用re.split()

import re
import itertools
str_list = ['There', 'is', ' a Red', '', 'shirt, but not a white One']
final_data = list(itertools.chain.from_iterable([re.split('\s(?=[A-Z])', i) for i in str_list]))

输出:

['There', 'is', ' a', 'Red', '', 'shirt, but not a white', 'One']
© www.soinside.com 2019 - 2024. All rights reserved.