minjeong / 3월 2주차 / 9문제 #2
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
백트래킹
최솟값 만들기 TIL: 역순 정렬
sortsort(reverse=True), sorted(reverse=Truereverse()/reversed())list.reverse(): list 자체를 역순으로 정렬, 함수 반환값 None 값reversed(list): 역순 정렬된 list 새로운 변수에 할당 가능귤 고르기 TIL: 딕셔너리 정렬
딕셔너리 정렬이 가물가물했다. 딕셔너리 정렬을 하면 좋을 것 같다고 생각이 들었는데, 딕셔너리는 key-value이니까 정렬을 못한다는 기억이 언저리 있었다.
하지만 딕셔너리도 key기준으로 또는 value를 기준으로 오름차순/내림차순 정렬을 할 수 있었다.
1. Key를 기준으로 정렬 (오름차순)
다음과 같이
sorted()를 이용하여 dict를 정렬할 수 있다.sorted()에 인자로my_dict.items()를 전달하면 오름차순으로 정렬됨sorted()는 다음과 같이 Tuple pair로 이루어진 List를 리턴함참고로,
my_dict.items()를 출력해보면 다음과 같이 Tuple pair로 이루어진 List가 리턴Output:
2. Key를 기준으로 정렬 (내림차순)
내림차순으로 정렬하려면
sorted()에reverse = True를 인자로 전달해야 한다.item[0]는 dict의 key를 의미함Output:
3. Value를 기준으로 정렬 (오름차순)
다음과 같이
sorted()를 사용하여 Value를 기준으로 정렬할 수 있다.item[1]은 dict의 Value를 의미합니다.Output:
4. Value를 기준으로 정렬 (내림차순)
내림차순으로 정렬하려면 다음과 같이
sorted()에 인자로reverse = True를 전달하면 된다.Output: