Hướng dẫn how do i fetch a word from a string in python? - làm cách nào để tìm nạp một từ từ một chuỗi trong python?

Đôi khi chúng ta đi qua các tình huống mà chúng ta yêu cầu để có được tất cả các từ có trong chuỗi, đây có thể là một nhiệm vụ tẻ nhạt được thực hiện bằng phương pháp gốc. Do đó có tốc ký để thực hiện nhiệm vụ này luôn hữu ích. Ngoài ra, bài viết này cũng bao gồm các trường hợp trong đó các dấu chấm câu phải bị bỏ qua. Phương pháp nếu người ta muốn hoàn thành nhiệm vụ cụ thể này. Nhưng nhược điểm là nó thất bại trong các trường hợp chuỗi chứa dấu chấm câu. & NBSP;
Method #1 : Using split() 
Using the split function, we can split the string into a list of words and this is the most generic and recommended method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.
 

Python3

test_string = "Geeksforgeeks is best Computer Science Portal"

print ("The original string is : " ________ 10 & nbsp;

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
1

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
2=
>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
4

print (

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
7 ________ 10 & nbsp;
>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
9
>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
0

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
The original string is : Geeksforgeeks is best Computer Science Portal 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 

& nbsp; & nbsp; Phương pháp #2: Sử dụng regex (findall ()) & nbsp; Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm kiếm thông thường Biểu thức để thực hiện nhiệm vụ này. Hàm Findall Trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua các dấu chấm câu. & nbsp;
Method #2 : Using regex( findall() ) 
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and extracting words ignoring punctuation marks.
 

Python3

>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
1
>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
2

test_string =

>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
5

print ("The original string is : " ________ 10 & nbsp;

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
1

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
2=
>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
4

print (

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
7 ________ 10 & nbsp;
>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
9
>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
0

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 

& nbsp; & nbsp; Phương pháp #2: Sử dụng regex (findall ()) & nbsp; Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm kiếm thông thường Biểu thức để thực hiện nhiệm vụ này. Hàm Findall Trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua các dấu chấm câu. & nbsp;
Method #3 : Using regex() + string.punctuation 
This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.
 

Python3

>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
1
>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
2

test_string =

>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
5

test_string =

>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
5

print ("The original string is : " ________ 10 & nbsp;

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
1

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
2=
>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
4

print (

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
7 ________ 10 & nbsp;
>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '
9
>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']
0

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 


Đã hỏi 5 năm, 7 tháng trước 5 years, 7 months ago

Đã xem 38k lần 38k times

-4

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi có chuỗi hình thức

 sent="Software Development = 1831".

Tôi chỉ muốn trích xuất các từ từ chuỗi "Phát triển phần mềm". Làm thế nào tôi có thể trích xuất điều này trong Python.

Đã hỏi ngày 17 tháng 3 năm 2017 lúc 9:46Mar 17, 2017 at 9:46

Hướng dẫn how do i fetch a word from a string in python? - làm cách nào để tìm nạp một từ từ một chuỗi trong python?

2

Bạn có thể thử "Geeksforgeeks is best Computer Science Portal"9:

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '

Đã trả lời ngày 17 tháng 3 năm 2017 lúc 9:48Mar 17, 2017 at 9:48

Hướng dẫn how do i fetch a word from a string in python? - làm cách nào để tìm nạp một từ từ một chuỗi trong python?

Harsha Biyaniharsha BiyaniHarsha Biyani

6,8589 Huy hiệu vàng35 Huy hiệu bạc57 Huy hiệu Đồng9 gold badges35 silver badges57 bronze badges

2

>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']

Đã trả lời ngày 17 tháng 3 năm 2017 lúc 9:51Mar 17, 2017 at 9:51

Hướng dẫn how do i fetch a word from a string in python? - làm cách nào để tìm nạp một từ từ một chuỗi trong python?

Ban đầu bạn có khoảng trống giữa print0, do đó mã của bạn sẽ trông như thế này

sent="Software Development = 1831"
answer=sent.split(" = ")[0] # space before and after =

Đã trả lời ngày 17 tháng 3 năm 2017 lúc 9:57Mar 17, 2017 at 9:57

Hướng dẫn how do i fetch a word from a string in python? - làm cách nào để tìm nạp một từ từ một chuỗi trong python?

Deepak sdeepak sDeepak S

451 Huy hiệu bạc10 Huy hiệu đồng1 silver badge10 bronze badges

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

Phương pháp số 1: Sử dụng split () Sử dụng hàm chia, chúng ta có thể chia chuỗi thành một danh sách các từ và đây là phương thức chung và được đề xuất nhất nếu người ta muốn hoàn thành nhiệm vụ cụ thể này.Using split() Using the split function, we can split the string into a list of words and this is the most generic and recommended method if one wished to accomplish this particular task.

Làm thế nào để tôi tìm thấy một từ trong một chuỗi trăn?

Chuỗi tìm () trong python chỉ cần gọi phương thức trên đối tượng chuỗi để tìm kiếm một chuỗi, như vậy: obj.find (tìm kiếm tìm kiếm).Phương thức Find () tìm kiếm một chuỗi truy vấn và trả về vị trí ký tự nếu được tìm thấy.Nếu chuỗi không được tìm thấy, nó sẽ trả về -1.obj. find(“search”). The find() method searches for a query string and returns the character position if found. If the string is not found, it returns -1.

Làm thế nào để bạn nhận được một giá trị cụ thể trong một chuỗi trong Python?

Cú pháp của chuỗi python tìm ()..
Subring: Bộ nền mà bạn muốn tìm kiếm trong chuỗi đã cho ..
Bắt đầu: (Tùy chọn) Giá trị bắt đầu từ nơi tìm kiếm cho chuỗi con sẽ bắt đầu.Theo mặc định, nó là 0 ..
Kết thúc: (Tùy chọn) Giá trị cuối trong đó tìm kiếm cho chuỗi con sẽ kết thúc.Theo mặc định, giá trị là độ dài của chuỗi ..

Làm cách nào để tìm một từ trong một chuỗi?

Để tìm một từ trong chuỗi, chúng tôi đang sử dụng các phương thức indexof () và chứa () của lớp chuỗi.Phương thức indexof () được sử dụng để tìm một chỉ mục của chuỗi con được chỉ định trong chuỗi hiện tại.Nó trả về một số nguyên dương dưới dạng một chỉ mục nếu cơ sở tìm thấy khác trả về -1.using indexOf() and contains() methods of String class. The indexOf() method is used to find an index of the specified substring in the present string. It returns a positive integer as an index if substring found else returns -1.