如何检查字符串列表中的字符串中是否存在大写字母? [英] How do I check if there is a uppercase letter in a string within a list of strings?

查看:139
本文介绍了如何检查字符串列表中的字符串中是否存在大写字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单词列表:

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

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

I want to check if there is a capital letter in every word in the list and if so I want to make a new list like this:

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]

但它不检查每个字母,而是检查所有字母是否在字符串中.

but it does not checking each letter but all of them in a string.

推荐答案

您可以使用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']

这篇关于如何检查字符串列表中的字符串中是否存在大写字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆