Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python - 주어진 문자열 목록의 하위 문자열인 모든 문자열 찾기

<시간/>

주어진 문자열 목록의 부분 문자열인 모든 문자열을 찾아야 할 때 'set' 및 'list' 속성이 사용됩니다.

예시

아래는 동일한 데모입니다.

my_list_1 = ["Hi", "there", "how", "are", "you"]
my_list_2 = ["Hi", "there", "how", "have", "you", 'been']
print("The first list is :")
print(my_list_1)
print("The second list is :")
print(my_list_2)

my_result = list(set([elem_1 for subset_1 in my_list_1 for elem_1 in my_list_2 if elem_1 in subset_1]))

print("The result is :")
print(my_result)

출력

The first list is :
['Hi', 'there', 'how', 'are', 'you']
The second list is :
['Hi', 'there', 'how', 'have', 'you', 'been']
The result is :
['there', 'you', 'Hi', 'how']

설명

  • 두 개의 문자열 목록이 정의되어 콘솔에 표시됩니다.

  • 두 목록이 반복되고 'set' 속성을 사용하여 목록에서 고유한 값을 가져옵니다.

  • 이제 목록으로 변환됩니다.

  • 이것은 변수에 할당됩니다.

  • 콘솔에 표시되는 출력입니다.