# 행열 형태의 리스트
list_of_lists = [
["안녕", "하세요"],
["저는", "빙입니다", "멈춤"],
["문자가", "나오면"],
["여기서", "멈춥니다", "그리고", "다시", "시작합니다"]
]
# 찾고자 하는 문자열
target1 = "멈춤"
target2 = "여기서"
# 멈춤이나온 행의 인덱스와 여기서가 나온 행의 인덱스를 찾음
start_index = -1
end_index = -1
for i, row in enumerate(list_of_lists):
if target1 in row:
start_index = i
if target2 in row:
end_index = i
# 인덱스가 유효한지 확인
if start_index == -1 or end_index == -1:
print(f"문자열 '{target1}' 또는 '{target2}'이(가) 리스트에 없습니다.")
else:
# 리스트 슬라이싱을 사용하여 새로운 리스트에 넣음
new_list = list_of_lists[start_index:end_index+1]
print(f"새로운 리스트는 {new_list}입니다.")
슬라이식
2023. 10. 31. 10:11