How to extract all UPPER from a string? Python

假装没事ソ 提交于 2019-12-18 13:05:17

问题


#input
my_string = 'abcdefgABCDEFGHIJKLMNOP'

how would one extract all the UPPER from a string?

#output
my_upper = 'ABCDEFGHIJKLMNOP'

回答1:


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'



回答2:


import string
s = 'abcdefgABCDEFGHIJKLMNOP'
s.translate(None,string.ascii_lowercase)

string.translate(s, table[, deletechars]) function will delete all characters from the string that are in deletechars, a list of characters. Then, the string will be translated using table (we are not using it in this case).

To remove only the lower case letters, you need to pass string.ascii_lowercase as the list of letters to be deleted.

The table is None because when the table is None, only the character deletion step will be performed.




回答3:


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".




回答4:


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



回答5:


You could use a more functional approach

>>> s = 'abcdefgABCDEFGHIJKLMNOP'
>>> filter(str.isupper, s)
'ABCDEFGHIJKLMNOP'



回答6:


here you go:

my_string = 'abcdefgABCDEFGHIJKLMNOP'

cleanChar = ''

for char in my_string:
    if char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        cleanChar = cleanChar + char

newChar = cleanChar
print(" {}".format(newChar))


来源:https://stackoverflow.com/questions/15886340/how-to-extract-all-upper-from-a-string-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!