如何在字符串中搜索大写字母并返回带大写字母和不带大写字母的单词列表 [英] how to search for a capital letter within a string and return the list of words with and without capital letters

查看:69
本文介绍了如何在字符串中搜索大写字母并返回带大写字母和不带大写字母的单词列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的家庭作业是编写一个程序,从用户那里读取一个字符串并从输入中创建一个单词列表.创建两个列表,一个包含至少包含一个大写字母的单词和一个单词不包含任何大写字母.用一个for循环打印出有大写字母的单词,然后是没有大写字母的单词,每行一个单词.

我所知道的不正确:

s= input("请输入您的字符串:")单词 = sorted(s.strip().split())逐字逐句:打印(字)

因为它只在 Capitol 位于第一个字符时对序列进行排序.对于此分配,字符可以出现在单词内的任何位置.例如,'tHis is a sString'.

我正在尝试一个看起来与此类似的解决方案,只是想看看我是否可以用大写字母来表达......但它不起作用:

 s = input("请输入一句话:")为真:上限 = 0s = s.strip().split()对于 c in s:如果 c 在ABCDEFGHIJKLMNOPQRSTUVWXYZ"中:打印(c[:cap])上限 += 1别的:打印(不是答案")休息

但是正则表达式可能比写出整个字母表做得更好.

非常感谢任何帮助.不用说,我是 Python 新手.

解决方案

>>>w = 'AbcDefgHijkL'>>>r = re.findall('([A-Z])', word)>>>r['A', 'D', 'H', 'L']

这可以让你在一个单词中用大写字母...只是分享想法

<预><代码>>>>r = re.findall('([A-Z][a-z]+)', w)>>>r['ABC'、'Defg'、'Hijk']

以上将为您提供所有以大写字母开头的单词.注意:最后一个没有被捕获,因为它不会产生一个词,但即使是可以捕获的

<预><代码>>>>r = re.findall('([A-Z][a-z]*)', w)>>>r['ABC'、'Defg'、'Hijk'、'L']

如果在单词中找到大写字母,这将返回真:

<预><代码>>>>字 = 'abcdD'>>>bool(re.search('([A-Z])', word))

My homework assignment is to Write a program that reads a string from the user and creates a list of words from the input.Create two lists, one containing the words that contain at least one upper-case letter and one of the words that don't contain any upper-case letters. Use a single for loop to print out the words with upper-case letters in them, followed by the words with no upper-case letters in them, one word per line.

What I know is not correct:

s= input("Enter your string: ")
words = sorted(s.strip().split())
for word in words:
    print (word)

Because it only sorts the sequence if the Capitol is in the first character. For this assignment a character could appear any where within a word. Such as, 'tHis is a sTring'.

I was playing around with a solution that looked similar to this, just to see if I could get the words with CAPS out..But it just wasnt working:

    s = input("Please enter a sentence: ")
while True:
    cap = 0
    s = s.strip().split()
    for c in s:
        if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            print(c[:cap])
            cap += 1
    else:
        print("not the answer")
        break 

But a regular expression would probably do a better job than writing out the whole alphabet.

Any help is much appreciated. Needless to say I am new to python.

解决方案

>>> w = 'AbcDefgHijkL'
>>> r = re.findall('([A-Z])', word)
>>> r
['A', 'D', 'H', 'L']

This can give you all letters in caps in a word...Just sharing the idea

>>> r = re.findall('([A-Z][a-z]+)', w)
>>> r
['Abc', 'Defg', 'Hijk']

Above will give you all words starting with Caps letter. Note: Last one not captured as it does not make a word but even that can be captured

>>> r = re.findall('([A-Z][a-z]*)', w)
>>> r
['Abc', 'Defg', 'Hijk', 'L']

This will return true if capital letter is found in the word:

>>> word = 'abcdD'
>>> bool(re.search('([A-Z])', word))

这篇关于如何在字符串中搜索大写字母并返回带大写字母和不带大写字母的单词列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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