Hướng dẫn python if any string in list matches - python nếu bất kỳ chuỗi nào trong danh sách khớp

Vấn đề là any[] trả về True nếu bất kỳ một trong các phần tử nào trong ITBEBER là True, vì vậy mã của bạn liên tục lặp lại miễn là câu trả lời không bằng tất cả các chuỗi trong ____ 10 có lẽ trái ngược với những gì bạn muốn xảy ra. Đây là một cách để sử dụng nó dừng hoặc phá vỡ vòng lặp nếu câu trả lời phù hợp với bất kỳ chuỗi nào:one of the elements in the iterable is True, so your code keeps looping as long as the answer isn't equal to all the strings in

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
0—which is probably the opposite of what you want to happen. Here's a way to use it that stops or breaks-out of the loop if the answer matches any of the strings:

months_list = ["January", "February", "March", "April", "May", "June", "July"]

while True:
    answer = raw_input["Month? "]
    if any[item.lower[] == answer.lower[] for item in months_list]: 
        break
    print["Sorry, didn't recognize your answer, try again"]

Như những người khác đã chỉ ra, trong khi việc sử dụng toán tử

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1 của Python sẽ đơn giản hơn, nhưng theo cách đó vẫn dẫn đến tìm kiếm tuyến tính, O [N], được thực hiện, vì vậy một cách tiếp cận thậm chí còn tốt hơn [nhanh hơn] sẽ là sử dụng
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
2 của thấp hơn CASED
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
3, sẽ sử dụng tra cứu dựa trên bảng băm, O [1], thay vì tìm kiếm tuyến tính:O[n], being performed…so an even better [faster] approach would be to use a
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
2 of lower-cased
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
3, which would utilize a hash table based look-up, O[1], instead of a linear search:

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]

Tinh chỉnh hơn nữa

Tùy thuộc vào bản chất của các chuỗi liên quan và lý do tại sao bạn so sánh chúng, có thể tốt hơn là sử dụng phương thức chuỗi

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
4 thay vì
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
5 để thực hiện so sánh chuỗi vô dụng.

Chúng tôi được cung cấp một chuỗi và nhiệm vụ của chúng tôi là kiểm tra xem chuỗi có chứa các phần tử từ danh sách không.

Example:

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True

Cách tiếp cận ngây thơ kiểm tra từng từ trong chuỗi

Ở đây chúng tôi đang chia chuỗi thành danh sách các từ và sau đó khớp từng từ của danh sách này với danh sách các từ đã hiện tại chúng tôi muốn kiểm tra.

Python3

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
8

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
9
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
2223
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
5

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
8

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3any[]1

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7any[]5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1

any[]7any[]1

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7True2
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1

Output:

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element

Sử dụng danh sách hiểu & nbsp; để kiểm tra xem chuỗi có chứa phần tử từ danh sách không to check if string contains element from list

Vấn đề này có thể được giải quyết bằng cách sử dụng danh sách hiểu, trong đó, chúng tôi kiểm tra danh sách và cả với các phần tử chuỗi nếu chúng tôi có thể tìm thấy một trận đấu và trả về true, nếu chúng tôi tìm thấy một và sai là không sử dụng các câu lệnh có điều kiện. & NBSP;
 

Python3

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
8

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
9
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
2223
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
5

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
8

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3

Output:

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
 to check if string contains element from list

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3any[]1
 

Python3

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
8

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
9
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
2223
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
5

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
8

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3

Output:

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3any[]1

Python3

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
8

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
9
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
2223
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
5

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
82
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
84

months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
85
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
8

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
94
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
96
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
98

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
85
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
9
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
05
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
98

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
82
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
7True

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
9
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1
months = set[month.lower[] for month in ["January", "February", "March", "April",
                                         "May", "June", "July"]]
while True:
    answer = raw_input["Month? "]
    if answer.lower[] in months: 
        break
    print["Sorry, didn't recognize your answer, try again"]
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3

Output:

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True

Làm thế nào để bạn kiểm tra xem một chuỗi có chứa trong danh sách Python?

Python Tìm chuỗi trong danh sách bằng cách sử dụng Count [], chúng ta cũng có thể sử dụng hàm Count [] để có được số lần xuất hiện của một chuỗi trong danh sách. Nếu đầu ra của nó là 0, thì điều đó có nghĩa là chuỗi không có trong danh sách.count[] We can also use count[] function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list.

Làm thế nào để bạn kiểm tra xem một từ trong danh sách có trong một chuỗi trăn không?

Sử dụng bất kỳ [] để kiểm tra xem chuỗi có chứa phần tử từ danh sách không.Sử dụng bất kỳ chức năng nào là cách cổ điển nhất mà bạn có thể thực hiện nhiệm vụ này và cũng hiệu quả.Hàm này kiểm tra đối sánh trong chuỗi với khớp của từng phần tử của danh sách. to check if string contains element from list. Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.

Làm cách nào để kiểm tra xem danh sách chuỗi có chứa chuỗi không?

Sử dụng phương thức String.Contains [] cho mỗi chuỗi con.Bạn có thể chấm dứt vòng lặp trong trận đấu đầu tiên của chuỗi con hoặc tạo chức năng tiện ích trả về true nếu chuỗi được chỉ định chứa bất kỳ chuỗi con nào từ danh sách được chỉ định. contains[] method for each substring. You can terminate the loop on the first match of the substring, or create a utility function that returns true if the specified string contains any of the substrings from the specified list.

Làm thế nào để bạn kiểm tra xem danh sách có chứa bất kỳ giá trị nào từ danh sách khác không?

Sử dụng bất kỳ [] cùng với biểu thức máy phát: list1 = ['item1', 'item2', 'item3'] list2 = ['item4', 'item5', 'item3'] nếu có]: In ["Bản sao được tìm thấy."] Khác: In ["Không tìm thấy bản sao."]: list1 = ['item1','item2','item3'] list2 = ['item4','item5','item3'] if any[x in list1 for x in list2]: print["Duplicates found."] else: print["No duplicates found."]

Bài Viết Liên Quan

Chủ Đề