Hướng dẫn how do you search for two words in a string in python? - làm thế nào để bạn tìm kiếm hai từ trong một chuỗi trong python?

Tôi muốn kiểm tra xem 2 từ "xe" và "xe máy" có ở mỗi yếu tố của một mảng trong Python hay không. Tôi biết cách kiểm tra một từ với

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
3 nhưng không biết làm thế nào để làm với 2 từ. Thực sự đánh giá cao bất kỳ sự giúp đỡ

Hỏi ngày 17 tháng 10 năm 2016 lúc 14:05Oct 17, 2016 at 14:05

Hướng dẫn how do you search for two words in a string in python? - làm thế nào để bạn tìm kiếm hai từ trong một chuỗi trong python?

2

Hai giải pháp từ:

for string in array:
    if 'car' in string and 'motorbike' in string.split():
        print("Car and motorbike are in string")

Giải pháp từ n Để kiểm tra xem tất cả các từ trong

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
4 có trong
test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
5 không:all words in
test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
4 are in
test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
5:

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")

Đã trả lời ngày 17 tháng 10 năm 2016 lúc 14:15Oct 17, 2016 at 14:15

Michel Touwmichel TouwMichel Touw

6266 Huy hiệu bạc15 Huy hiệu Đồng6 silver badges15 bronze badges

3

Sử dụng một boolean phụ trợ.

car=False
 motorbike=False
 for elem in array:

        if "car" in elem:
            car=True
        if "motorbike" in elem:
            motorbike=True
        if car and motorbike:
            break

Chỉnh sửa: Tôi chỉ đọc "trong mỗi yếu tố". Chỉ cần sử dụng và.

Đã trả lời ngày 17 tháng 10 năm 2016 lúc 14:16Oct 17, 2016 at 14:16

Hướng dẫn how do you search for two words in a string in python? - làm thế nào để bạn tìm kiếm hai từ trong một chuỗi trong python?

1

Tôi nghĩ rằng một giải pháp đơn giản là:

all(map(lambda w: w in text, ('car', 'motorbike')))

Nhưng có thể có một vấn đề với điều này, tùy thuộc vào mức độ kén chọn bạn cần so sánh:

>>> text = 'Can we buy motorbikes in carshops?'
>>> all(map(lambda w: w in text, ('car', 'motorbike')))
True

Các từ 'xe' và 'xe máy' không có trong

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
6, và điều này vẫn nói
test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
7. Bạn có thể cần một trận đấu đầy đủ trong các từ. Tôi sẽ làm điều này:

>>> words = ('car', 'motorbike')
>>> text = 'Can we buy motorbikes in carshops?'
>>> set(words).issubset(text.split())
False
>>> text = 'a car and a motorbike'
>>> set(words).issubset(text.split())
True

Và bây giờ nó hoạt động!

Đã trả lời ngày 17 tháng 10 năm 2016 lúc 14:33Oct 17, 2016 at 14:33

Hướng dẫn how do you search for two words in a string in python? - làm thế nào để bạn tìm kiếm hai từ trong một chuỗi trong python?

ldavidldavidldavid

2.4822 Huy hiệu vàng20 Huy hiệu bạc38 Huy hiệu Đồng2 gold badges20 silver badges38 bronze badges

1

Tôi sẽ sử dụng chức năng

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
8:

wanted_values = ("car", "motorbike")
all(vehicle in text for text in wanted_values)

Vì vậy, nếu chúng ta có một danh sách các chuỗi:

l = ['some car and motorbike',
     'a motorbike by a car',
     'the car was followed by a motorbike']

lines_with_vehicles = [text for text in l
                       if all(vehicle in text for text in wanted_values)]

Với Regex, bạn có thể làm:

# no particular order
car_and_motorbike_pattern = re.compile(r'(car.*motorbike|motorbike.*car)')
all(car_and_motorbike_pattern.search(text) for text in list_of_expressions)

# This works too
car_or_motorbike_pattern = re.compile(r'(car|motorbike)')
get_vehicles = car_or_motorbike_pattern.findall
all(len(set(get_vehicles(text))) == 2 for text in list_of_expressions)

Đã trả lời ngày 24 tháng 7 năm 2017 lúc 10:59Jul 24, 2017 at 10:59

Hướng dẫn how do you search for two words in a string in python? - làm thế nào để bạn tìm kiếm hai từ trong một chuỗi trong python?

l__flex__ll__flex__ll__flex__l

1.2681 Huy hiệu vàng15 Huy hiệu bạc21 Huy hiệu đồng1 gold badge15 silver badges21 bronze badges

Liên kết này làm việc cho tôi: nó cung cấp 3 giải pháp. Hai phương pháp sử dụng toàn bộ danh sách và phương pháp thứ ba sử dụng các hàm MAP + LAMBDA.


Tôi nghĩ rằng không có một cách dễ dàng và pythonic để làm điều đó. Bạn cần sử dụng logic xấu xí như tiếp theo:

image_file_name = 'man_in_car.jpg'
if 'car' in image_file_name and 'man' in image_file_name:
    print('"car" and "man" were found in the image_file_name')

Điều đó sẽ hoạt động cho hai từ, nhưng nếu bạn cần kiểm tra nhiều từ, thì hãy sử dụng mã tốt hơn trong liên kết trên


Tôi muốn có thể làm một cái gì đó như:

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
0

Or:

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
1

Nhưng 2 đoạn mã cuối cùng không hoạt động trong Python (chưa).

Đã trả lời ngày 9 tháng 7 năm 2020 lúc 9:27Jul 9, 2020 at 9:27

Hướng dẫn how do you search for two words in a string in python? - làm thế nào để bạn tìm kiếm hai từ trong một chuỗi trong python?

JavijaviJavi

7041 Huy hiệu vàng8 Huy hiệu bạc14 Huy hiệu đồng1 gold badge8 silver badges14 bronze badges

1

Sử dụng chức năng này để kiểm tra hai hoặc nhiều từ khóa trong một câu với các toán tử 'và' hoặc 'hoặc'.

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")
2

Đã trả lời ngày 17 tháng 3 lúc 17:42Mar 17 at 17:42

Hướng dẫn how do you search for two words in a string in python? - làm thế nào để bạn tìm kiếm hai từ trong một chuỗi trong python?

HenulhenulHenul

1501 Huy hiệu bạc8 Huy hiệu đồng1 silver badge8 bronze badges