Cách nhanh nhất/sạch nhất để loại bỏ từ đầu tiên của chuỗi là gì? Tôi biết tôi có thể sử dụng split
và sau đó lặp lại trên mảng để lấy chuỗi của tôi. Nhưng tôi khá chắc chắn rằng đó không phải là cách tốt nhất để làm điều đó.
Tái bút: Tôi khá mới với Python và tôi không biết mọi thủ thuật.
Cảm ơn trước sự giúp đỡ của bạn.
DDA
5,8142 Huy hiệu vàng24 Huy hiệu bạc34 Huy hiệu đồng2 gold badges24 silver badges34 bronze badges
Hỏi ngày 14 tháng 10 năm 2012 lúc 14:57Oct 14, 2012 at 14:57
Matthieu Rieglermatthieu RieglerMatthieu Riegler
23.6K17 Huy hiệu vàng83 Huy hiệu bạc123 Huy hiệu đồng17 gold badges83 silver badges123 bronze badges
0
Tôi nghĩ rằng cách tốt nhất là phân chia, nhưng chỉ giới hạn nó ở chỉ một chia bằng cách cung cấp tham số
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
0:>>> s = 'word1 word2 word3'
>>> s.split[' ', 1]
['word1', 'word2 word3']
>>> s.split[' ', 1][1]
'word2 word3'
Đã trả lời ngày 14 tháng 10 năm 2012 lúc 14:59Oct 14, 2012 at 14:59
3
Một giải pháp ngây thơ sẽ là:
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
Tuy nhiên, điều đó sẽ không hoạt động theo ví dụ sau [thừa nhận đã thừa nhận]:
text = "Hi,nice people"
print text.partition[' '][2] # people
Để xử lý việc này, bạn sẽ cần biểu thức thường xuyên:
import re
print re.sub[r'^\W*\w+\W*', '', text]
Tổng quát hơn, không thể trả lời một câu hỏi liên quan đến "từ" mà không biết chúng ta đang nói ngôn ngữ tự nhiên nào. "J'ai" có bao nhiêu từ? Làm thế nào về "中华 和国"?
Đã trả lời ngày 14 tháng 10 năm 2012 lúc 15:08Oct 14, 2012 at 15:08
Georggeorggeorg
207K48 Huy hiệu vàng296 Huy hiệu bạc375 Huy hiệu Đồng48 gold badges296 silver badges375 bronze badges
Câu trả lời khác sẽ nêu ra một ngoại lệ nếu chuỗi của bạn chỉ có một từ, mà tôi cho rằng không phải là điều bạn muốn.
Một cách để làm điều này thay vào đó là sử dụng chức năng
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
1.>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
Đã trả lời ngày 14 tháng 10 năm 2012 lúc 15:08Oct 14, 2012 at 15:08
GeorggeorgJulian
207K48 Huy hiệu vàng296 Huy hiệu bạc375 Huy hiệu Đồng14 silver badges25 bronze badges
Câu trả lời khác sẽ nêu ra một ngoại lệ nếu chuỗi của bạn chỉ có một từ, mà tôi cho rằng không phải là điều bạn muốn.
>>> test = "word1 word2 word3"
>>> test.partition[" "]
['word1', ' ', 'word2 word3']
Một cách để làm điều này thay vào đó là sử dụng chức năng
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
1.JulianjulianOct 14, 2012 at 15:07
3.25514 Huy hiệu bạc25 Huy hiệu đồngGareth Latty
Giả sử bạn có thể đảm bảo các từ được phân tách bằng một không gian,
2 là những gì bạn đang tìm kiếm.16 gold badges178 silver badges178 bronze badgestext = "funny cheese shop"
print text.partition[' '][2] # cheese shop
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;
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
3text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
4 text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
5text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6 text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
7text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
8text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
9text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6 text = "Hi,nice people"
print text.partition[' '][2] # people
1text = "Hi,nice people"
print text.partition[' '][2] # people
2 text = "Hi,nice people"
print text.partition[' '][2] # people
3
4text = "Hi,nice people"
print text.partition[' '][2] # people
4 text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6text = "Hi,nice people"
print text.partition[' '][2] # people
7text = "Hi,nice people"
print text.partition[' '][2] # people
8text = "Hi,nice people"
print text.partition[' '][2] # people
9____________text = "Hi,nice people"
print text.partition[' '][2] # people
9text = "Hi,nice people"
print text.partition[' '][2] # people
2import re
print re.sub[r'^\W*\w+\W*', '', text]
The original string is : GeeksforGeeks is best The string after omitting first word is : is best
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
8import re
print re.sub[r'^\W*\w+\W*', '', text]
4text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6 text = "Hi,nice people"
print text.partition[' '][2] # people
1text = "Hi,nice people"
print text.partition[' '][2] # people
2 import re
print re.sub[r'^\W*\w+\W*', '', text]
8import re
print re.sub[r'^\W*\w+\W*', '', text]
9 Method
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
import re
print re.sub[r'^\W*\w+\W*', '', text]
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
text = "Hi,nice people"
print text.partition[' '][2] # people
text = "Hi,nice people"
print text.partition[' '][2] # people
import re
print re.sub[r'^\W*\w+\W*', '', text]
import re
print re.sub[r'^\W*\w+\W*', '', text]
Đầ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;
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
8>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
7text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
3text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
4 >>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
2>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
3text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6 >>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
5>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
8>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
9text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6 >>> test = "word1 word2 word3"
>>> test.partition[" "]
['word1', ' ', 'word2 word3']
1text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
8>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
7>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
8text = "Hi,nice people"
print text.partition[' '][2] # people
2 text = "Hi,nice people"
print text.partition[' '][2] # people
3text = "Hi,nice people"
print text.partition[' '][2] # people
4text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
4 >>> test = "word1 word2 word3"
>>> test.partition[" "]
['word1', ' ', 'word2 word3']
7text = "Hi,nice people"
print text.partition[' '][2] # people
7import re
print re.sub[r'^\W*\w+\W*', '', text]
0The original string is : GeeksforGeeks is best The string after omitting first word is : is best0
import re
print re.sub[r'^\W*\w+\W*', '', text]
2
4text = "Hi,nice people"
print text.partition[' '][2] # people
4 text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6text = "Hi,nice people"
print text.partition[' '][2] # people
7text = "Hi,nice people"
print text.partition[' '][2] # people
8text = "Hi,nice people"
print text.partition[' '][2] # people
9____________text = "Hi,nice people"
print text.partition[' '][2] # people
9text = "Hi,nice people"
print text.partition[' '][2] # people
2import re
print re.sub[r'^\W*\w+\W*', '', text]
The original string is : GeeksforGeeks is best The string after omitting first word is : is best
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
8import re
print re.sub[r'^\W*\w+\W*', '', text]
4text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
6 text = "Hi,nice people"
print text.partition[' '][2] # people
1text = "Hi,nice people"
print text.partition[' '][2] # people
2 import re
print re.sub[r'^\W*\w+\W*', '', text]
8import re
print re.sub[r'^\W*\w+\W*', '', text]
9
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
import re
print re.sub[r'^\W*\w+\W*', '', text]
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
text = "Hi,nice people"
print text.partition[' '][2] # people
text = "Hi,nice people"
print text.partition[' '][2] # people
import re
print re.sub[r'^\W*\w+\W*', '', text]
import re
print re.sub[r'^\W*\w+\W*', '', text]
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;
The original string is : GeeksforGeeks is best The string after omitting first word is : is best3
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
4The original string is : GeeksforGeeks is best The string after omitting first word is : is best5
The original string is : GeeksforGeeks is best The string after omitting first word is : is best6
text = "Hi,nice people"
print text.partition[' '][2] # people
9The original string is : GeeksforGeeks is best The string after omitting first word is : is best8
text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
3text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
4 >>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
2
3>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
6 text = "funny cheese shop"
print text.partition[' '][2] # cheese shop
5>>> s = "foo bar baz"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition[" "]
>>> rest or first
'foo'
The original string is : GeeksforGeeks is best The string after omitting first word is : is best