고유한 값 목록이 있는 사전을 가져와야 하는 경우 간단한 반복과 함께 'set' 연산자와 목록 메서드를 사용합니다.
예시
아래는 동일한 데모입니다 -
my_dictionary = [{'Python' : 11, 'is' : 22}, {'fun' : 11, 'to' : 33}, {'learn' : 22},{'object':9},{'oriented':11}]
print("The dictionary is : " )
print(my_dictionary)
my_result = list(set(value for element in my_dictionary for value in element.values()))
print("The resultant list is : ")
print(my_result)
print("The resultant list after sorting is : ")
my_result.sort()
print(my_result) 출력
The dictionary is :
[{'Python': 11, 'is': 22}, {'fun': 11, 'to': 33}, {'learn': 22}, {'object': 9}, {'oriented': 11}]
The resultant list is :
[33, 11, 22, 9]
The resultant list after sorting is :
[9, 11, 22, 33] 설명
-
사전 목록이 정의되어 콘솔에 표시됩니다.
-
사전의 값은 반복하여 액세스하고 집합으로 변환합니다.
-
이렇게 하면 고유한 요소를 얻을 수 있습니다.
-
그런 다음 목록으로 변환되어 변수에 할당됩니다.
-
콘솔에 출력으로 표시됩니다.
-
다시 정렬되어 콘솔에 표시됩니다.