如何从字符串中提取所有 UPPER?Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15886340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:16:17  来源:igfitidea点击:

How to extract all UPPER from a string? Python

pythonstringextractuppercaselowercase

提问by O.rka

#input
my_string = 'abcdefgABCDEFGHIJKLMNOP'

how would one extract all the UPPER from a string?

如何从字符串中提取所有 UPPER?

#output
my_upper = 'ABCDEFGHIJKLMNOP'

采纳答案by piokuc

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'

回答by herinkc

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

string.translate(S,表[,deletechars])函数将从处于字符串删除所有字符deletechars,字符的列表。然后,字符串将使用 table 进行翻译(在这种情况下我们不使用它)

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

要仅删除小写字母,您需要传递string.ascii_lowercase作为要删除的字母列表。

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

table是无因为当表None中,只有字符删除步骤将被执行。

回答by Joran Beasley

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

所以这个 join 加上 findall 是最快的方法(每个 ipython %timeit (python 2.6)),使用 10000 个字符长的相同字符串

edit: Or not

编辑:或不

In [12]: %timeit  my_list.translate(None,string.ascii_lowercase)
10000 loops, best of 3: 51.6 us per loop

回答by hatkirby

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

编辑:如果您不知道过滤器的作用:过滤器接受一个函数和一个可迭代对象,然后将该函数应用于可迭代对象中的每个元素。它保留所有返回 true 的值并丢弃所有其余的值。因此,这将返回“ABCDEFGHIJKLMNOP”。

回答by Finn

You could use a more functional approach

您可以使用更实用的方法

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

回答by user9830600

here you go:

干得好:

my_string = 'abcdefgABCDEFGHIJKLMNOP'

cleanChar = ''

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

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