Hướng dẫn how do you get a specific word in a string in python? - làm thế nào để bạn có được một từ cụ thể trong 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

if 'seek' in 'those who seek shall find':
    print['Success!']
7
if 'seek' in 'those who seek shall find':
    print['Success!']
8
if 'seek' in 'those who seek shall find':
    print['Success!']
9

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
0
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
1
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
2 ________ 33 & nbsp;
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
4

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
5
if 'seek' in 'those who seek shall find':
    print['Success!']
8
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
7

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
0
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
1
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
0 ________ 33 & nbsp;
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
2
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
3

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

def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
4
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
5

if 'seek' in 'those who seek shall find':
    print['Success!']
7
if 'seek' in 'those who seek shall find':
    print['Success!']
8
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
8

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
0
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
1
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
2 ________ 33 & nbsp;
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
4

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
5
if 'seek' in 'those who seek shall find':
    print['Success!']
8
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
7

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
0
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
1
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
0 ________ 33 & nbsp;
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
2
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
3

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

def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
4
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
5

if 'seek' in 'those who seek shall find':
    print['Success!']
7
if 'seek' in 'those who seek shall find':
    print['Success!']
8
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
8

if 'seek' in 'those who seek shall find':
    print['Success!']
7
if 'seek' in 'those who seek shall find':
    print['Success!']
8
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
8

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
0
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
1
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
2 ________ 33 & nbsp;
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
4

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
5
if 'seek' in 'those who seek shall find':
    print['Success!']
8
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
7

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
0
import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
1
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
0 ________ 33 & nbsp;
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
2
def contains_word[s, w]:
    return [' ' + w + ' '] in [' ' + s + ' ']

contains_word['the quick brown fox', 'brown']  # True
contains_word['the quick brown fox', 'row']    # False
3

Đầ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’] 
 


230

433 huy hiệu đồng
Learn more.

Tôi tin rằng câu trả lời này gần với những gì ban đầu được hỏi: Tìm chuỗi con trong chuỗi nhưng chỉ khi toàn bộ từ?

Nó đang sử dụng một regex đơn giản:

if string.find[word]:
    print["success"]

mkrieger1

Đã trả lời ngày 25 tháng 8 năm 2021 lúc 13:254 gold badges46 silver badges57 bronze badges

Milos Cuculovicmilos CuculovicMar 16, 2011 at 1:10

1

Phim huy hiệu vàng 18,9k50

if word in mystring: 
   print['success']

Martin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Một trong những giải pháp là đặt một khoảng trống ở đầu và cuối của từ thử nghiệm. Điều này thất bại nếu từ ở đầu hoặc kết thúc câu hoặc nằm cạnh bất kỳ dấu câu nào. Giải pháp của tôi là viết một hàm thay thế bất kỳ dấu câu nào trong chuỗi kiểm tra bằng khoảng trắng và thêm một khoảng trống vào đầu và cuối hoặc chuỗi kiểm tra và từ kiểm tra, sau đó trả về số lần xuất hiện. Đây là một giải pháp đơn giản loại bỏ sự cần thiết cho bất kỳ biểu thức regex phức tạp nào.Mar 16, 2011 at 1:13

Để đếm số lần xuất hiện của một từ trong một chuỗi:fabrizioM

trả về 115 gold badges98 silver badges115 bronze badges

13

if 'seek' in 'those who seek shall find':
    print['Success!']

Sử dụng hàm trong 'if' để kiểm tra xem từ có tồn tại trong một chuỗi không

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None

Đã trả lời ngày 18 tháng 3 lúc 9:37Mar 16, 2011 at 1:52

IstuartistuartHugh Bothwell

3362 Huy hiệu bạc6 Huy hiệu Đồng7 gold badges81 silver badges98 bronze badges

6

Làm cách nào để trích xuất một từ duy nhất từ ​​một chuỗi?

Để có được từ đầu tiên của một chuỗi: gọi phương thức chia [], chuyển nó, một chuỗi chứa một khoảng trống dưới dạng tham số. Phương thức phân chia sẽ trả về một mảng chứa các từ trong chuỗi. Truy cập mảng tại chỉ mục 0 để có được từ đầu tiên của chuỗi.

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

>python -m timeit -s "def contains_word[s, w]: return [' ' + w + ' '] in [' ' + s + ' ']" "contains_word['the quick brown fox', 'brown']"
1000000 loops, best of 3: 0.351 usec per loop

>python -m timeit -s "import re" -s "def contains_word[s, w]: return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search[s]" "contains_word['the quick brown fox', 'brown']"
100000 loops, best of 3: 2.38 usec per loop

>python -m timeit -s "def contains_word[s, w]: return s.startswith[w + ' '] or s.endswith[' ' + w] or s.find[' ' + w + ' '] != -1" "contains_word['the quick brown fox', 'brown']"
1000000 loops, best of 3: 1.13 usec per loop

Nếu chúng ta muốn kiểm tra xem một chuỗi đã cho có chứa một từ được chỉ định trong đó hay không, chúng ta có thể sử dụng câu lệnh IF/IN trong Python. Câu lệnh IF/IN trả về true nếu từ có trong chuỗi và sai nếu từ không có trong chuỗi. Đầu ra cho thấy từ này có mặt bên trong chuỗi biến chuỗi. A slight variant on this idea for Python 3.6+, equally fast:

def contains_word[s, w]:
    return f' {w} ' in f' {s} '

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.Apr 11, 2016 at 20:32

user200783user200783user200783

Tôi đang làm việc với Python và tôi đang cố gắng tìm hiểu xem bạn có thể biết một từ có ở trong một chuỗi không.11 gold badges63 silver badges125 bronze badges

6

Tôi đã tìm thấy một số thông tin về việc xác định nếu từ có trong chuỗi - sử dụng

def find_words[text, search]:
    """Find exact words"""
    dText   = text.split[]
    dSearch = search.split[]

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len[dSearch]:
        return lenSearch
    else:
        return False
2, nhưng có cách nào để thực hiện câu lệnh
def find_words[text, search]:
    """Find exact words"""
    dText   = text.split[]
    dSearch = search.split[]

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len[dSearch]:
        return lenSearch
    else:
        return False
3. Tôi muốn có một cái gì đó như sau:

15.8k4 Huy hiệu vàng46 Huy hiệu bạc57 Huy hiệu Đồng

Martin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Một trong những giải pháp là đặt một khoảng trống ở đầu và cuối của từ thử nghiệm. Điều này thất bại nếu từ ở đầu hoặc kết thúc câu hoặc nằm cạnh bất kỳ dấu câu nào. Giải pháp của tôi là viết một hàm thay thế bất kỳ dấu câu nào trong chuỗi kiểm tra bằng khoảng trắng và thêm một khoảng trống vào đầu và cuối hoặc chuỗi kiểm tra và từ kiểm tra, sau đó trả về số lần xuất hiện. Đây là một giải pháp đơn giản loại bỏ sự cần thiết cho bất kỳ biểu thức regex phức tạp nào.Mar 16, 2011 at 1:13

Để đếm số lần xuất hiện của một từ trong một chuỗi:Matt Howell

trả về 17 gold badges47 silver badges56 bronze badges

0

Sử dụng hàm trong 'if' để kiểm tra xem từ có tồn tại trong một chuỗi không

if word in string.split[]:
    print["success"]

Martin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Đã trả lời ngày 18 tháng 3 lúc 9:37Dec 1, 2016 at 18:26

IstuartistuartCorvax

3362 Huy hiệu bạc6 Huy hiệu Đồng7 silver badges12 bronze badges

3

Làm cách nào để trích xuất một từ duy nhất từ ​​một chuỗi?

Cũng hỗ trợ tìm kiếm chuỗi Unicode.

def find_words[text, search]:
    """Find exact words"""
    dText   = text.split[]
    dSearch = search.split[]

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len[dSearch]:
        return lenSearch
    else:
        return False

usage:

if word in mystring: 
   print['success']
0

Đã trả lời ngày 22 tháng 6 năm 2012 lúc 22:51Jun 22, 2012 at 22:51

Guray Celikguray CelikGuray Celik

1.2751 Huy hiệu vàng14 Huy hiệu bạc13 Huy hiệu đồng1 gold badge14 silver badges13 bronze badges

0

Nếu khớp một chuỗi các ký tự là không đủ và bạn cần phải khớp với toàn bộ từ, thì đây là một chức năng đơn giản để hoàn thành công việc. Về cơ bản, nó nối các không gian khi cần thiết và tìm kiếm điều đó trong chuỗi:

if word in mystring: 
   print['success']
1

Điều này giả định rằng dấu phẩy và các dấu chấm câu khác đã bị loại bỏ.

IANS

15.2k9 Huy hiệu vàng57 Huy hiệu bạc81 Huy hiệu Đồng9 gold badges57 silver badges81 bronze badges

Đã trả lời ngày 15 tháng 6 năm 2012 lúc 7:23Jun 15, 2012 at 7:23

DasongdasongdaSong

4071 Huy hiệu vàng5 Huy hiệu bạc9 Huy hiệu đồng1 gold badge5 silver badges9 bronze badges

1

Sử dụng Regex là một giải pháp, nhưng nó quá phức tạp cho trường hợp đó.

Bạn chỉ có thể chia văn bản thành danh sách các từ. Sử dụng phương pháp tách [dấu phân cách, num] cho điều đó. Nó trả về một danh sách tất cả các từ trong chuỗi, sử dụng dấu phân cách làm dấu phân cách. Nếu dấu tách không xác định, nó chia tách trên tất cả khoảng trắng [tùy chọn bạn có thể giới hạn số lượng phân tách thành Num].split[separator, num] method for that. It returns a list of all the words in the string, using separator as the separator. If separator is unspecified it splits on all whitespace [optionally you can limit the number of splits to num].

if word in mystring: 
   print['success']
2

Điều này sẽ không hoạt động cho chuỗi với dấu phẩy, v.v. Ví dụ:

if word in mystring: 
   print['success']
3

Nếu bạn cũng muốn phân chia trên tất cả các dấu phẩy, v.v. Sử dụng đối số phân tách như sau:separator argument like this:

if word in mystring: 
   print['success']
4

Martin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Đã trả lời ngày 18 tháng 12 năm 2017 lúc 11:44Dec 18, 2017 at 11:44

Tstempkotstempkotstempko

1.1761 Huy hiệu vàng14 Huy hiệu bạc16 Huy hiệu đồng1 gold badge14 silver badges16 bronze badges

2

Khi bạn đang yêu cầu một từ và không phải cho một chuỗi, tôi muốn trình bày một giải pháp không nhạy cảm với tiền tố / hậu tố và bỏ qua trường hợp:

if word in mystring: 
   print['success']
5

Nếu lời nói của bạn có thể chứa các ký tự đặc biệt của Regex [chẳng hạn như

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
3], thì bạn cần
def find_words[text, search]:
    """Find exact words"""
    dText   = text.split[]
    dSearch = search.split[]

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len[dSearch]:
        return lenSearch
    else:
        return False
7

Đã trả lời ngày 9 tháng 8 năm 2017 lúc 10:11Aug 9, 2017 at 10:11

Martin Thomamartin ThomaMartin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Đã trả lời ngày 18 tháng 12 năm 2017 lúc 11:44

if word in mystring: 
   print['success']
6

Martin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Đã trả lời ngày 18 tháng 12 năm 2017 lúc 11:44Nov 2, 2016 at 8:39

TstempkotstempkoRameez

1.1761 Huy hiệu vàng14 Huy hiệu bạc16 Huy hiệu đồng5 silver badges11 bronze badges

Khi bạn đang yêu cầu một từ và không phải cho một chuỗi, tôi muốn trình bày một giải pháp không nhạy cảm với tiền tố / hậu tố và bỏ qua trường hợp:

if word in mystring: 
   print['success']
7

Nếu lời nói của bạn có thể chứa các ký tự đặc biệt của Regex [chẳng hạn như

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
3], thì bạn cần
def find_words[text, search]:
    """Find exact words"""
    dText   = text.split[]
    dSearch = search.split[]

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len[dSearch]:
        return lenSearch
    else:
        return False
7

if word in mystring: 
   print['success']
8

Đã trả lời ngày 9 tháng 8 năm 2017 lúc 10:11

if word in mystring: 
   print['success']
9

Sample:

if 'seek' in 'those who seek shall find':
    print['Success!']
0

Martin Thomamartin Thoma

Cách nâng cao để kiểm tra từ chính xác mà chúng ta cần tìm trong một chuỗi dài:Dec 26, 2020 at 5:18

Đã trả lời ngày 2 tháng 11 năm 2016 lúc 8:39marcio

Rameezrameez5 silver badges16 bronze badges

5345 huy hiệu bạc11 huy hiệu đồng

if 'seek' in 'those who seek shall find':
    print['Success!']
1

Điều gì sẽ phân chia chuỗi và dải dấu chấm câu?

if 'seek' in 'those who seek shall find':
    print['Success!']
2

Martin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Đã trả lời ngày 18 tháng 12 năm 2017 lúc 11:44Feb 26, 2015 at 14:23

TstempkotstempkoPyGuy

1.1761 Huy hiệu vàng14 Huy hiệu bạc16 Huy hiệu đồng3 bronze badges

1

Khi bạn đang yêu cầu một từ và không phải cho một chuỗi, tôi muốn trình bày một giải pháp không nhạy cảm với tiền tố / hậu tố và bỏ qua trường hợp:

Nếu lời nói của bạn có thể chứa các ký tự đặc biệt của Regex [chẳng hạn như

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
3], thì bạn cần
def find_words[text, search]:
    """Find exact words"""
    dText   = text.split[]
    dSearch = search.split[]

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len[dSearch]:
        return lenSearch
    else:
        return False
7

if 'seek' in 'those who seek shall find':
    print['Success!']
3

Martin Thoma

113K148 Huy hiệu vàng574 Huy hiệu bạc877 Huy hiệu đồng148 gold badges574 silver badges877 bronze badges

Đã trả lời ngày 18 tháng 12 năm 2017 lúc 11:44Aug 25, 2021 at 13:25

TstempkotstempkoMilos Cuculovic

1.1761 Huy hiệu vàng14 Huy hiệu bạc16 Huy hiệu đồng50 gold badges156 silver badges264 bronze badges

Khi bạn đang yêu cầu một từ và không phải cho một chuỗi, tôi muốn trình bày một giải pháp không nhạy cảm với tiền tố / hậu tố và bỏ qua trường hợp:

if 'seek' in 'those who seek shall find':
    print['Success!']
4

Nếu lời nói của bạn có thể chứa các ký tự đặc biệt của Regex [chẳng hạn như

import re

def findWholeWord[w]:
    return re.compile[r'\b[{0}]\b'.format[w], flags=re.IGNORECASE].search

findWholeWord['seek']['those who seek shall find']    # -> 
findWholeWord['word']['swordsmith']                   # -> None
3], thì bạn cần
def find_words[text, search]:
    """Find exact words"""
    dText   = text.split[]
    dSearch = search.split[]

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len[dSearch]:
        return lenSearch
    else:
        return False
7

if 'seek' in 'those who seek shall find':
    print['Success!']
5

Đã trả lời ngày 9 tháng 8 năm 2017 lúc 10:11

if 'seek' in 'those who seek shall find':
    print['Success!']
6

Đã trả lời ngày 9 tháng 8 năm 2017 lúc 10:11

Martin Thomamartin Thoma

Cách nâng cao để kiểm tra từ chính xác mà chúng ta cần tìm trong một chuỗi dài:Mar 18 at 9:37

Đã trả lời ngày 2 tháng 11 năm 2016 lúc 8:39iStuart

Rameezrameez2 silver badges6 bronze badges

Làm cách nào để trích xuất một từ duy nhất từ một chuỗi?

Để có được từ đầu tiên của một chuỗi: gọi phương thức chia [], chuyển nó, một chuỗi chứa một khoảng trống dưới dạng tham số.Phương thức phân chia sẽ trả về một mảng chứa các từ trong chuỗi.Truy cập mảng tại chỉ mục 0 để có được từ đầu tiên của chuỗi.Call the split[] method passing it a string containing an empty space as a parameter. The split method will return an array containing the words in the string. Access the array at index 0 to get the first word of the string.

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

Nếu chúng ta muốn kiểm tra xem một chuỗi đã cho có chứa một từ được chỉ định trong đó hay không, chúng ta có thể sử dụng câu lệnh IF/IN trong Python.Câu lệnh IF/IN trả về true nếu từ có trong chuỗi và sai nếu từ không có trong chuỗi.Đầu ra cho thấy từ này có mặt bên trong chuỗi biến chuỗi.use the if/in statement in Python. The if/in statement returns True if the word is present in the string and False if the word is not in the string. The output shows that the word is is present inside the string variable string .

Bài Viết Liên Quan

Chủ Đề