hoặc sử dụng regex ... đây là một câu trả lời dễ dàng
import re
print ''.join[re.findall['[A-Z]+',my_string]]
Chỉ để so sánh
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
Vì vậy, tham gia Plus Findall này là phương pháp nhanh nhất [trên mỗi thời gian IPYTHON %[Python 2.6]], sử dụng chuỗi giống hệt nhau 10000 ký tự
Chỉnh sửa: hoặc không: Or not
In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
Đầu ra
Phương pháp số 4: Sử dụng phương thức ord []. Giá trị ASCII của bảng chữ cái chữ hoa là từ 65 đến 90.
test_str
0=
test_str
2
test_str
7___Đầu ra
Cải thiện bài viết
Lưu bài viết
Đôi khi, trong khi làm việc với các chuỗi, chúng tôi lo ngại về độ nhạy của trường hợp của các chuỗi và có thể yêu cầu chỉ nhận được một trường hợp cụ thể của ký tự trong một chuỗi dài. Hãy để thảo luận về những cách nhất định trong đó chỉ có thể trích xuất các chữ cái chữ hoa từ một chuỗi.
Phương pháp số 1: Sử dụng danh sách hiểu + isupper [] & nbsp;
Danh sách hiểu và chức năng isupper có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Độ hiểu hiểu danh sách chủ yếu được sử dụng để lặp lại trong danh sách và chức năng isupper kiểm tra các ký tự chữ hoa. & NBSP;
Python3
test_str
=
"GeeksForGeeKs"
____10
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
1In [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
2 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
3In [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
4 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
5In [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
6In [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
7=
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
9In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
0 In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
1In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
2____10
In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
7In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
2 In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
9In [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
4 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
5The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']2
Đầu ra: & nbsp;
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Độ phức tạp về thời gian: O [n] O[n]
Độ phức tạp không gian: O [n] O[n]
Phương pháp số 2: Sử dụng Filter [] + Lambda
Chức năng lọc cùng với chức năng Lambda có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Hàm bộ lọc thực hiện lựa chọn cụ thể của các ký tự trường hợp và chức năng Lambda được sử dụng cho chuỗi truyền tải. & NBSP;
Python3
test_str
=
"GeeksForGeeKs"
____10
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
1In [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
2 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
3In [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
4 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
5In [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
6In [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
7=
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
9In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
0 In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
1In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
2____10
In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
7In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
2 In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
9In [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
4 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
5The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']2
Đầu ra: & nbsp;
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Độ phức tạp về thời gian: O [n]O[n]
Độ phức tạp không gian: O [n] O[n]
Phương pháp số 2: Sử dụng Filter [] + Lambda
Python3
test_str
=
"GeeksForGeeKs"
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
7=
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']5
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']6
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']7
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']6
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']9
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']0
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']7
=
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']9
test_str
0=
test_str
2
Phương pháp số 3: Không sử dụng các phương thức tích hợp
____10
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']6
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']3
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
4 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
5In [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
6In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
0 test_str
4In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
2 test_str
6test_str
7
In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
4 test_str
4In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
2 =
1=
2=
3
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Độ phức tạp về thời gian: O [n] O[n]
Độ phức tạp không gian: O [n] O[n]
Phương pháp số 2: Sử dụng Filter [] + Lambda Using ord[] method.The ASCII value of the uppercase alphabet is from 65 to 90.
Python3
test_str
=
"GeeksForGeeKs"
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
7=
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']5
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']6
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']7
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']6
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']9
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']0
Phương pháp số 3: Không sử dụng các phương thức tích hợp
Phương pháp số 3: Không sử dụng các phương thức tích hợp
____10
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']6
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']3
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
4 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
5In [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
6=
2=
3
test_str
7
In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
4 test_str
4In [12]: %timeit my_list.translate[None,string.ascii_lowercase]
10000 loops, best of 3: 51.6 us per loop
2 =
1=
2=
3
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Độ phức tạp về thời gian: O [n]O[n]
Độ phức tạp không gian: O [n]O[n]