Created
March 15, 2019 07:29
-
-
Save kayalshri/f484b1e205437adfe6af4ee314cdf134 to your computer and use it in GitHub Desktop.
Python :: Count Unique Elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Problem : Get Number of unique elements in a list | |
x = ['f', 'e', 'e', 'f', 'f'] | |
# output {'e': 2, 'f': 3} | |
##Solution | |
o = {i:x.count(i) for i in x} | |
print(o) | |
## Problem : Get Number of unique values in a dict | |
y = {'a': 1, 'c': 5, 'b': 4, 'd': 6, 'e':5, 'f':4, 'g':1, 'h':5} | |
# output {1: 2, 4: 2, 5: 3, 6: 1} | |
## Solution | |
o = {i:y.values().count(i) for i in y.values()} | |
print(o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment