Hướng dẫn python extract uppercase from string - python trích xuất chữ hoa từ chuỗi

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___
  • Làm thế nào để bạn trích xuất các chữ cái chữ hoa từ một chuỗi trong Python?
  • Đầ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
    
    1
    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
    
    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
    
    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
    
    5
    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
    
    6

    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=
    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
    
    9
    In [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
    
    1
    In [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
    
    7
    In [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
    
    9
    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
    
    5
    The 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
    
    1
    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
    
    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
    
    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
    
    5
    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
    
    6

    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=
    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
    
    9
    In [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
    
    1
    In [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
    
    7
    In [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
    
    9
    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
    
    5
    The 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
    
    5
    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
    
    6

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

    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 4
    In [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
    
    5
    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
    
    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 4
    In [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)


    Làm thế nào để bạn trích xuất các chữ cái chữ hoa từ một chuỗi trong Python?

    Phương thức giá trị trả về () trả về () Phương thức trả về chuỗi chữ hoa từ chuỗi đã cho.Nó chuyển đổi tất cả các ký tự thường thành chữ hoa. Return Value upper() method returns the uppercase string from the given string. It converts all lowercase characters to uppercase.

    Làm thế nào chúng ta có thể tìm thấy chữ hoa trong chuỗi?

    Cách tiếp cận :..
    Quét chuỗi str từ 0 đến chiều dài-1 ..
    Kiểm tra một ký tự tại một thời điểm trên cơ sở các giá trị ASCII.if (str [i]> = 65 và str [i] = 97 và str [i] = 48 và str [i]
    In tất cả các bộ đếm ..