How to extract all UPPER from a string? Python

后端 未结 7 2064
终归单人心
终归单人心 2020-12-31 04:51
#input
my_string = \'abcdefgABCDEFGHIJKLMNOP\'

how would one extract all the UPPER from a string?

#output
my_upper = \'ABCDEFGHIJKL         


        
相关标签:
7条回答
  • 2020-12-31 05:19

    Using list comprehension:

    >>> s = 'abcdefgABCDEFGHIJKLMNOP'
    >>> ''.join([c for c in s if c.isupper()])
    'ABCDEFGHIJKLMNOP'
    

    Using generator expression:

    >>> ''.join(c for c in s if c.isupper())
    'ABCDEFGHIJKLMNOP
    

    You can also do it using regular expressions:

    >>> re.sub('[^A-Z]', '', s)
    'ABCDEFGHIJKLMNOP'
    
    0 讨论(0)
  • 2020-12-31 05:22

    here you go:

    my_string = 'abcdefgABCDEFGHIJKLMNOP'
    
    cleanChar = ''
    
    for char in my_string:
        if char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
            cleanChar = cleanChar + char
    
    newChar = cleanChar
    print(" {}".format(newChar))
    
    0 讨论(0)
  • 2020-12-31 05:27

    Higher order functions to the rescue!

    filter(str.isupper, "abcdefgABCDEFGHIJKLMNOP")
    

    EDIT: In case you don't know what filter does: filter takes a function and an iterable, and then applies the function to every element in the iterable. It keeps all of the values that return true and throws out all of the rest. Therefore, this will return "ABCDEFGHIJKLMNOP".

    0 讨论(0)
  • 2020-12-31 05:30

    or use regex ... this is an easy answer

    import re
    print ''.join(re.findall('[A-Z]+',my_string))
    

    just for comparison

    In [6]: %timeit filter(str.isupper,my_list)
    1000 loops, best of 3: 774 us per loop
    
    In [7]: %timeit ''.join(re.findall('[A-Z]+',my_list))
    1000 loops, best of 3: 563 us per loop
    
    In [8]: %timeit re.sub('[^A-Z]', '', my_list)
    1000 loops, best of 3: 869 us per loop
    
    In [10]: %timeit ''.join(c for c in my_list if c.isupper())
    1000 loops, best of 3: 1.05 ms per loop
    

    so this join plus findall is the fastest method (per ipython %timeit (python 2.6)) , using a 10000 character long identical string

    edit: Or not

    In [12]: %timeit  my_list.translate(None,string.ascii_lowercase)
    10000 loops, best of 3: 51.6 us per loop
    
    0 讨论(0)
  • 2020-12-31 05:30
    for char in my_string:
         if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
             print(char)
    
    0 讨论(0)
  • 2020-12-31 05:36

    You could use a more functional approach

    >>> s = 'abcdefgABCDEFGHIJKLMNOP'
    >>> filter(str.isupper, s)
    'ABCDEFGHIJKLMNOP'
    
    0 讨论(0)
提交回复
热议问题