[英]How do I check if there is a uppercase letter in a string within a list of strings?
I have a list of words: 我有一个词表:
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”]
I tried: 我试过了:
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. 但它不检查每个字母,而是检查所有字母是否在一个字符串中。
You can use re.split()
: 您可以使用
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]))
Output: 输出:
['There', 'is', ' a', 'Red', '', 'shirt, but not a white', 'One']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.