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
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'
9In [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'
1In [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'
3In [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'
4In [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'
5In [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'
7In [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
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]
21st 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]
8In [115]: ' '.join['a b \tc\nd'.split[]]
Out[115]: 'a b c d'
41st 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]
51st 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]
61st 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]
51st 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]
81st 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]
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'
4The original string is : GeeksforGeeks is best
The string after omitting first word is : is best
0In [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'
7In [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
4The original string is : GeeksforGeeks is best
The string after omitting first word is : is best
5 Method
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'
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'
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'
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'
Đầ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'
4The original string is : GeeksforGeeks is best The string after omitting first word is : is best3
In [115]: ' '.join['a b \tc\nd'.split[]]
Out[115]: 'a b c d'
9In [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 best8
The original string is : GeeksforGeeks is best The string after omitting first word is : is best9
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 best1
The original string is : GeeksforGeeks is best The string after omitting first word is : is best4
The original string is : GeeksforGeeks is best The string after omitting first word is : is best55
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 best7
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'
4The original string is : GeeksforGeeks is best The string after omitting first word is : is best3
The original string is : GeeksforGeeks is best The string after omitting first word is : is best4
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'
91st 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 best3
In [115]: ' '.join['a b \tc\nd'.split[]]
Out[115]: 'a b c d'
81st 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]
6The original string is : GeeksforGeeks is best The string after omitting first word is : is best6
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
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]
21st 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]
8In [115]: ' '.join['a b \tc\nd'.split[]]
Out[115]: 'a b c d'
41st 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]
51st 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]
61st 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]
51st 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]
81st 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]
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'
4The original string is : GeeksforGeeks is best
The string after omitting first word is : is best
0In [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'
7In [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
4The original string is : GeeksforGeeks is best
The string after omitting first word is : is best
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'
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'
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'
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'
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;
' '.join
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#
1#
21st 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#
4In [115]: ' '.join['a b \tc\nd'.split[]]
Out[115]: 'a b c d'
9In [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 best8
The original string is : GeeksforGeeks is best
The string after omitting first word is : is best
9
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'
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