Hướng dẫn how do i remove a word from a python beginning? - làm cách nào để xóa một từ khỏi đầu python?

Một vấn đề với việc sử dụng split ở đây là nó loại bỏ khoảng trắng. Ví dụ,

In [114]: 'a  b \tc\nd'.split[]
Out[114]: ['a', 'b', 'c', 'd']

Vì vậy, việc tham gia lại với nhau một lần nữa với ' '.join làm thay đổi chuỗi ban đầu:

In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'

Nếu bạn muốn bảo tồn chuỗi ban đầu và chỉ cần xóa các từ bắt đầu bằng #, thì bạn có thể sử dụng regex:

In [119]: import re

In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
Out[120]: 'Hello all please help    but#notme'

Explanation::

//regex101.com có ​​một công cụ tiện dụng để hỗ trợ bạn tìm hiểu các biểu thức thường xuyên. Ví dụ, đây là lời giải thích của nó cho những gì

In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
0 có nghĩa là:

1st Capturing group [\s]
    \s match any white space character [\r\n\t\f ]
# matches the character # literally
\w+ match any word character [a-zA-Z0-9_]
    Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]

Vì mẫu Regex này bắt đầu bằng cách khớp với khoảng trắng,

In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
1 phù hợp, nhưng
In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
2 thì không.

Đối số thứ hai cho

In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
3,
In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
4, là mẫu thay thế.
In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
5 bảo
In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
3 thay thế trận đấu bằng nhóm bắt đầu đầu tiên. Vì vậy, trận đấu
In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
1 được thay thế bằng không gian
In [115]: ' '.join['a  b \tc\nd'.split[]]
Out[115]: 'a b c d'
8.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Trong quá trình lập trình, đôi khi, chúng ta có thể có một vấn đề như vậy trong đó yêu cầu từ đầu tiên từ chuỗi phải được xóa. Những loại vấn đề này là phổ biến và người ta nên biết về giải pháp cho các vấn đề như vậy. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết.

    Python3

    Phương pháp số 1: Sử dụng phương thức Split []

    Nhiệm vụ này có thể được thực hiện bằng cách sử dụng hàm chia thực hiện phân chia các từ và tách từ đầu tiên của chuỗi với toàn bộ từ. & Nbsp;

    In [115]: ' '.join['a  b \tc\nd'.split[]]
    Out[115]: 'a b c d'
    
    9
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    0
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    1
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    2
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    3

    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    4
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    5
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    2
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    7
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    8
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    9

    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    0____2020
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    2
    In [115]: ' '.join['a  b \tc\nd'.split[]]
    Out[115]: 'a b c d'
    
    8
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    4
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    5
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    6
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    5
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    8

    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best

    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    0
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    2
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    7
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    8
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    5
    Method 

    Đầu ra: & nbsp;

    Python3

    Phương thức số 2: Sử dụng Phương thức [] Phương thức & NBSP;

    Hàm phân vùng được sử dụng để thực hiện nhiệm vụ cụ thể này trong các tác vụ nội bộ tương đối ít hơn so với hàm được sử dụng trong phương thức trên. & NBSP;

    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    3

    In [115]: ' '.join['a  b \tc\nd'.split[]]
    Out[115]: 'a b c d'
    
    9
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    0
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    8

    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    9
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    2
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    1

    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    55
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    2
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    7

    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    3

    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    4
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    8
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    9

    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    0____2020
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    3
    In [115]: ' '.join['a  b \tc\nd'.split[]]
    Out[115]: 'a b c d'
    
    8
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    6
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    6
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    8

    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    0____2020
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    2
    In [115]: ' '.join['a  b \tc\nd'.split[]]
    Out[115]: 'a b c d'
    
    8
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    4
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    5
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    6
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    5
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    8

    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best

    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    0
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    2
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    7
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    8
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    5

    Python3

    Đầu ra: & nbsp;

    Phương thức số 2: Sử dụng Phương thức [] Phương thức & NBSP;

    Hàm phân vùng được sử dụng để thực hiện nhiệm vụ cụ thể này trong các tác vụ nội bộ tương đối ít hơn so với hàm được sử dụng trong phương thức trên. & NBSP;

    ' '.join9

    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    0#1#2
    1st Capturing group [\s]
        \s match any white space character [\r\n\t\f ]
    # matches the character # literally
    \w+ match any word character [a-zA-Z0-9_]
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    
    5#4

    In [115]: ' '.join['a  b \tc\nd'.split[]]
    Out[115]: 'a b c d'
    
    9
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    0
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    8

    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    9
    In [119]: import re
    
    In [120]: re.sub[r'[\s]#\w+', r'\1', 'Hello all please help #me   but#notme']
    Out[120]: 'Hello all please help    but#notme'
    
    2
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    1

    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best


    Làm cách nào để loại bỏ một ký tự từ đầu chuỗi trong Python?

    Sử dụng phương thức .Strip [] để loại bỏ khoảng trắng và các ký tự từ đầu và phần cuối của chuỗi. Sử dụng . Phương thức lStrip [] để loại bỏ khoảng trắng và ký tự chỉ từ đầu chuỗi. strip[] method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip[] method to remove whitespace and characters only from the beginning of a string.

    Làm cách nào để loại bỏ một từ khỏi câu python?

    Chúng ta có thể sử dụng hàm thay thế [] để xóa từ khỏi chuỗi trong python.Hàm này thay thế một chuỗi con nhất định với chuỗi con được đề cập.Chúng ta có thể thay thế một từ bằng một ký tự trống để loại bỏ nó.Chúng tôi cũng có thể chỉ định có bao nhiêu lần xuất hiện của một từ chúng tôi muốn thay thế trong hàm.use the replace[] function to remove word from string in Python. This function replaces a given substring with the mentioned substring. We can replace a word with an empty character to remove it. We can also specify how many occurrences of a word we want to replace in the function.

    Làm thế nào để bạn xóa văn bản khỏi một chuỗi trong Python?

    Bạn có thể xóa một ký tự khỏi chuỗi python bằng cách sử dụng thay thế [] hoặc dịch [].Cả hai phương thức này thay thế một ký tự hoặc chuỗi bằng một giá trị đã cho.Nếu một chuỗi trống được chỉ định, ký tự hoặc chuỗi bạn chọn sẽ bị xóa khỏi chuỗi mà không cần thay thế.using replace[] or translate[]. Both these methods replace a character or string with a given value. If an empty string is specified, the character or string you select is removed from the string without a replacement.

    Làm cách nào để loại bỏ một từ cụ thể khỏi một chuỗi?

    Cho một chuỗi và một từ mà tác vụ xóa từ khỏi chuỗi nếu nó tồn tại khác trả về -1 dưới dạng đầu ra ...
    Chuyển đổi chuỗi thành một mảng chuỗi ..
    Lặp lại mảng và kiểm tra từ không bằng từ đã cho ..
    Concatenate từ vào một tên mảng chuỗi mới dưới dạng chuỗi mới ..
    In chuỗi mới ..

    Bài Viết Liên Quan

    Chủ Đề