简体   繁体   中英

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() :

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM