【问题标题】:Checking for an uppercase in strings inside a list检查列表内字符串中的大写字母
【发布时间】:2018-04-15 21:37:18
【问题描述】:

我的任务是执行一项小任务 对于仅包含字符串的列表,我需要检查以下内容并创建一个列表,如果字符串中有大写字母,我需要隔离该单词并将其作为单独的单词放入新列表中, 例如列表

str_list = [“This”, “is”, “not a Red”, “”, “wine, but a white One”]

新的应该是这样的

split_str_list = [“这个”、“是”、“不是一个”、“红色”、“”、“酒,而是一个白色”、“一个”]

非常感谢您的帮助

【问题讨论】:

  • 请分享您尝试过的代码,并准确发布您面临的问题和错误消息。乐于助人!

标签: string python-2.7 list uppercase lowercase


【解决方案1】:
str_list = ["This","is","not a Red","","Wine, but a white One"]
new_list=[]
string=''
for i in str_list:
    print i
    words=i.split(' ')
    print words
    for x in words:
        if x == ' ' or x=='':
            new_list.append(x)

        elif x[0].isupper() and string != '':
            string=string.strip()
            new_list.append(string)
            new_list.append(x)
            string=''
        elif x[0].isupper() and string == '':
            new_list.append(x)

        else:
            string=string+x+' '
print new_list

输出:['This', 'is not a', 'Red', '', 'Wine,', 'but a white', 'One']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    相关资源
    最近更新 更多