목록은 요소로 튜플을 가질 수 있습니다. 이 기사에서는 문자열인 특정 검색 요소를 포함하는 튜플을 식별하는 방법을 배웁니다.
상태 및 상태
우리는 조건에 따라 디자인할 수 있습니다. 에서 조건 또는 조건의 조합을 언급할 수 있습니다.
예
listA = [('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
test_elem = 'Mon'
#Given list
print("Given list:\n",listA)
print("Check value:\n",test_elem)
# Uisng for and if
res = [item for item in listA
if item[0] == test_elem and item[1] >= 2]
# printing res
print("The tuples satisfying the conditions:\n ",res) 출력
위의 코드를 실행하면 다음과 같은 결과가 나옵니다. -
Given list:
[('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
Check value:
Mon
The tuples satisfying the conditions:
[('Mon', 3), ('Mon', 2)] 필터 포함
우리는 Lambda 함수와 함께 필터 함수를 사용합니다. 필터 조건에서 in 연산자를 사용하여 튜플에 요소가 있는지 확인합니다.
예
listA = [('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
test_elem = 'Mon'
#Given list
print("Given list:\n",listA)
print("Check value:\n",test_elem)
# Uisng lambda and in
res = list(filter(lambda x:test_elem in x, listA))
# printing res
print("The tuples satisfying the conditions:\n ",res) 출력
위의 코드를 실행하면 다음과 같은 결과가 나옵니다. -
Given list:
[('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)]
Check value:
Mon
The tuples satisfying the conditions:
[('Mon', 3), ('Mon', 2)]