목록 목록에서 값을 검색하고 바꿔야합니다.중첩 목록의 값을 검색하고 바꿀 수있는 방법
내 현재 코드는 내가 그것을 할 필요가 다음 더 복잡하다고 느낀다 그러나 작동 : 나는에 함께 답변을 자갈길했습니다. 이 일을하는보다 우아한 방법이 있습니까?
# Create test data- a list of lists which each contain 2 items
numbers = list(range(10))
list_of_lists = [numbers[i:i+2] for i in range(0, len(numbers), 2)]
# Flatten the list of lists
flat_list = [item for sublist in list_of_lists for item in sublist]
# Search for and replace values
modified_list = [-1 if e > 5 else e for e in flat_list]
# Regroup into a list of lists
regrouped_list_of_lists = [modified_list[i:i+2] for i in range(0, len(modified_list), 2)]
이것은 아마도 그것을하는 가장 Pythonic 방법입니다. 작동하고 읽을 수있는 경우 수정하지 마세요. – TheLazyScripter
@TheLazyScripter * 작동하고 읽을 수있는 경우 수정하지 마세요 *이 경우가 아닙니다. –