Hướng dẫn how to find letters in a list python - cách tìm các chữ cái trong danh sách python

Tôi có một vấn đề nêu rõ: viết một hàm lấy, như một đối số, một danh sách và một chuỗi và trả về một boolean dựa trên việc liệu tất cả các chữ cái trong chuỗi có xuất hiện ở đâu đó trong danh sách hay không. Đây là những gì tôi có cho đến nay.

def findLetters[myList, myString]:
    for letter in myString:
        if letter in myList:
            return True
    return False

101

8.1763 Huy hiệu vàng38 Huy hiệu bạc65 Huy hiệu Đồng3 gold badges38 silver badges65 bronze badges

Hỏi ngày 26 tháng 9 năm 2017 lúc 0:39Sep 26, 2017 at 0:39

2

Đây là một giải pháp cơ bản gần với những gì bạn đã bắt đầu:

def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 

Đã trả lời ngày 26 tháng 9 năm 2017 lúc 0:56Sep 26, 2017 at 0:56

Bạn có thể ánh xạ tất cả các chữ cái bằng Lambda tạo ra danh sách các giá trị boolean cho tất cả các chữ cái trong

def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
1.

Hàm

def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
2 trả về
def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
3 nếu tất cả các giá trị trong danh sách
def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
4 AR
def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
5.

def find_letters[my_list, my_string]: 
    l = map[lambda x: x in my_list, my_string]
    return all[l]

print[find_letters[['a', 'b', 'c'], 'cab']]

Đã trả lời ngày 26 tháng 9 năm 2017 lúc 0:49Sep 26, 2017 at 0:49

Bạn đang trả về đúng nếu có bất kỳ chữ cái nào phù hợp trong MyString, không phải tất cả các chữ cái trong MyString. Bạn có thể làm theo cách khác, nếu bất kỳ chữ cái nào không khớp trong myString, hãy trả lại sai

def findLetters[myList, myString]:
    for letter in myString:
        if letter not in myList:
            return False
    return True

Hoặc sử dụng chức năng tích hợp tất cả

def findLetters[myList, myString]:
  return all[letter in myList for letter in myString]

Đã trả lời ngày 26 tháng 9 năm 2017 lúc 0:47Sep 26, 2017 at 0:47

SkyccskyccSkycc

3,4601 Huy hiệu vàng12 Huy hiệu bạc18 Huy hiệu đồng1 gold badge12 silver badges18 bronze badges

Chúng ta có thể sử dụng toán tử Python

def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
6 để kiểm tra xem một chuỗi có có trong danh sách hay không. Ngoài ra còn có một toán tử
def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
7 để kiểm tra xem một chuỗi không có trong danh sách.

l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']

# string in the list
if 'A' in l1:
    print['A is present in the list']

# string not in the list
if 'X' not in l1:
    print['X is not present in the list']

Output:

A is present in the list
X is not present in the list

Đọc được đề xuất: Python F-Strings, hãy xem xét một ví dụ khác, nơi chúng tôi sẽ yêu cầu người dùng nhập chuỗi để kiểm tra danh sách.

l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']
s = input['Please enter a character A-Z:\n']

if s in l1:
    print[f'{s} is present in the list']
else:
    print[f'{s} is not present in the list']

Output:

________số 8

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.

l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']
s = 'A'

count = l1.count[s]
if count > 0:
    print[f'{s} is present in the list for {count} times.']

Tìm tất cả các chỉ mục của một chuỗi trong danh sách

Không có chức năng tích hợp để có được danh sách tất cả các chỉ mục của một chuỗi trong danh sách. Dưới đây là một chương trình đơn giản để có được danh sách tất cả các chỉ mục trong đó chuỗi có trong danh sách.

def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
0

Đầu ra:

def findLetters[myList, myString]:
    found_all = False
    for s in myString:        # check each letter in the string
        if s in myList:       # see if it is in the list
            found_all = True  # keep going if found
        else:
            found_all = False # otherwise set `found` to False 
            break             # and break out of the loop

    return found_all          # return the result

result = findLetters[['a', 'l', 'i', 's', 't'], 'mlist']

# 'm' is not in the list
print result # False 

# all letters in the string are in the list; 
# ignores any extra characters in the list that are not in the string
result = findLetters[['a', 'l', 'i', 's', 't', 'p'], 'alist']

print result # True 
8

Bạn có thể kiểm tra toàn bộ tập lệnh Python và nhiều ví dụ về Python từ Kho lưu trữ GitHub của chúng tôi.

Bài Viết Liên Quan

Chủ Đề