Hướng dẫn python remove first word from string - python xóa từ đầu tiên khỏi chuỗi

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.

Hướng dẫn python remove first word from string - python xóa từ đầu tiên khỏi chuỗi

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

Hướng dẫn python remove first word from string - python xóa từ đầu tiên khỏi chuỗi

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

Hướng dẫn python remove first word from string - python xóa từ đầu tiên khỏi chuỗi

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,

text = "funny cheese shop"
print text.partition(' ')[2] # cheese shop
2 là những gì bạn đang tìm kiếm.16 gold badges178 silver badges178 bronze badges

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;

    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    3
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    4
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    5
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    6
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    7

    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    8
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    9
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    6
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    1
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    2
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    3

    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    4
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    4
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    6
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    7
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    8
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    9____________
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    9
    import re
    print re.sub(r'^\W*\w+\W*', '', text)
    
    2

    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 

    Đầ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'
    
    7

    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    3
    text = "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'
    
    3
    text = "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'
    
    9
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    6
    >>> test = "word1 word2 word3"
    >>> test.partition(" ")
    ('word1', ' ', 'word2 word3')
    
    1

    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'
    
    7

    >>> s = "foo bar baz"
    >>> first, _, rest = s.partition(" ")
    >>> rest or first
    'bar baz'
    
    >>> s = "foo"
    >>> first, _, rest = s.partition(" ")
    >>> rest or first
    'foo'
    
    8
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    2
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    3

    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    4
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    4
    >>> test = "word1 word2 word3"
    >>> test.partition(" ")
    ('word1', ' ', 'word2 word3')
    
    7
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    7
    import re
    print re.sub(r'^\W*\w+\W*', '', text)
    
    0
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    0
    import re
    print re.sub(r'^\W*\w+\W*', '', text)
    
    2

    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    4
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    4
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    6
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    7
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    8
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    9____________
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    9
    import re
    print re.sub(r'^\W*\w+\W*', '', text)
    
    2

    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

    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 best
    3
    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    4
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    5
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    6
    text = "Hi,nice people"
    print text.partition(' ')[2] # people
    
    9
    The original string is : GeeksforGeeks is best
    The string after omitting first word is : is best
    8

    text = "funny cheese shop"
    print text.partition(' ')[2] # cheese shop
    
    3
    text = "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'
    
    3
    text = "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

    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ỏ từ đầu tiên khỏi một chuỗi trong Python?

    Phương thức 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ừ.Using split() Method This task can be performed using the split function which performs a split of words and separates the first word of string with the entire words.

    Làm cách nào để loại bỏ từ đầu tiên khỏi một chuỗi?

    Để xóa từ đầu tiên khỏi chuỗi, hãy gọi phương thức indexof () để lấy chỉ mục của không gian đầu tiên trong chuỗi. Sau đó, sử dụng phương thức chuỗi con () để lấy một phần của chuỗi, với từ đầu tiên bị xóa.Đã sao chép!call the indexOf() method to get the index of the first space in the string. Then use the substring() method to get a portion of the string, with the first word removed. Copied!

    Làm thế nào để bạn xóa chữ cái đầu tiên trong Python?

    Python cung cấp các chức năng sẵn có khác nhau, cắt lát () là một trong số đó.Nếu chúng ta muốn xóa ký tự đầu tiên hoặc một số char khác khỏi chuỗi Python, chúng ta có thể xóa ký tự đó bằng phương pháp cắt và sau đó lấy chuỗi kết quả không bao gồm ký tự đầu tiên.erase that character using the slicing method and then get the resultant string excluding the first character.