how to search for a capital letter within a string and return the list of words with and without capital letters

前端 未结 9 1186
无人共我
无人共我 2020-12-18 23:23

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 con

相关标签:
9条回答
  • 2020-12-18 23:24
    >>> 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))
    
    0 讨论(0)
  • 2020-12-18 23:32

    Sounds like regexs would be easier for the first part of the problem (a regex that just looks for [A-Z] should do the trick).

    For the second part, I'd recommend using two lists, as that's an easy way to print everything out in one for loop. Have one list of non_upper_words and one of upper_words.

    So, the basic outline of the program would be:

    1. split the string into an array of words.
    2. for each word in array: if regex returns true, add to upper_words. Else: add to non_upper_words.
    3. print each word in the first array and then in the second.

    I wrote this out in pseudo-code because it's a programming assignment, so you should really write the actual code yourself. Hope it helps!

    0 讨论(0)
  • 2020-12-18 23:37

    You can use List Comprehensions to get all upper case characters and lower case characters.

    def get_all_cap_lowercase_list(inputStr):
        cap_temp_list = [c for c in inputStr if c.isupper()]
        low_temp_list = [c for c in inputStr if c.islower()]
        print("List of Cap {0}  and List of Lower {1}".format(cap_temp_list,low_temp_list))
    
        upper_case_count = len(cap_temp_list)
        lower_case_count = len(low_temp_list)
        print("Count of Cap {0}  and Count of Lower {1}".format(upper_case_count,lower_case_count))
    get_all_cap_lowercase_list("Hi This is demo Python program")
    

    And The output is:

    List of Cap ['H', 'T', 'P'] and List of Lower ['i', 'h', 'i', 's', 'i', 's', 'd', 'e', 'm', 'o', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm']

    Count of Cap 3 and Count of Lower 22

    0 讨论(0)
  • 2020-12-18 23:39

    You can use isupper method for your purpose:

    text = 'sssample Text with And without'
    
    uppers = []
    lowers = []
    
    # Note that loop below could be modified to skip ,.-\/; and etc if neccessary
    for word in text.split():
        uppers.append(word) if word[0].isupper() else lowers.append(word)
    

    EDITED: You can also use islower method the following way:

    text = 'sssample Text with And without'
    
    other = []
    lowers = []
    
    # Note that loop below could be modified to skip ,.-\/; and etc if neccessary
    for word in text.split():
        lowers.append(word) if word.islower() else other.append(word)
    

    OR depends on what you really need you can take a look at istitle method:

    titled = []
    lowers = []
    
    for word in text.split():
        titled.append(word) if word.istitle() else lower.append(word)
    

    AND with simple if else statement:

    titled = []
    lowers = []
    
    for word in text.split():       
        if word.istitle():
            titled.append(word) 
        else:
            lower.append(word)
    
    0 讨论(0)
  • 2020-12-18 23:39

    I think your answer might only be searching for words where the first letter is capitalized. To find words that contain a capital letter anywhere in the word, you'd have to enumerate over each letter in the word, like this:

    uin = input("Enter your text: ")
    
    ##create lists to hold upper and lower case words
    up_words = []
    no_up_words = []
    
    for i, word in enumerate(uin.strip().split()):
        if word.islower():
            no_up_words.append(word)
        else: 
            up_words.append(word)
    
    print(up_words, no_up_words)
    
    0 讨论(0)
  • 2020-12-18 23:43

    Try doing the following:

    1. Split the string into a list where each item is a separate word.
    2. For every word in that list, iterate through and check for capital letters (consider the string constants such as string.uppercase). If it has a capital letter, insert it onto the front of your result list. If not, append it to the end of your result list.
    3. Iterate through your results, printing them. Or, if you want to avoid iterating, join the items in the string using the newline character \n.
    0 讨论(0)
提交回复
热议问题