confused on using a set to count duplicates /u/Same-Criticism4335 Python Education

If a set doesn’t contain duplicates, then how does this work? I’m missing some key details of a set.

def count_duplicates(i): unique_elements = set(i) duplicates = [] for element in unique_elements: if i.count(element) > 1: duplicates.append((element, i.count(element))) return i, unique_elements, duplicates my_str = 'fuzz' a,b,c = count_duplicates(my_str) print(a) print(b) print(c) [OUTPUT] fuzz # initial string {'f', 'u', 'z'} # set from string [('z', 2)] # dict of duplicates with count 

submitted by /u/Same-Criticism4335
[link] [comments]

​r/learnpython If a set doesn’t contain duplicates, then how does this work? I’m missing some key details of a set. def count_duplicates(i): unique_elements = set(i) duplicates = [] for element in unique_elements: if i.count(element) > 1: duplicates.append((element, i.count(element))) return i, unique_elements, duplicates my_str = ‘fuzz’ a,b,c = count_duplicates(my_str) print(a) print(b) print(c) [OUTPUT] fuzz # initial string {‘f’, ‘u’, ‘z’} # set from string [(‘z’, 2)] # dict of duplicates with count submitted by /u/Same-Criticism4335 [link] [comments] 

If a set doesn’t contain duplicates, then how does this work? I’m missing some key details of a set.

def count_duplicates(i): unique_elements = set(i) duplicates = [] for element in unique_elements: if i.count(element) > 1: duplicates.append((element, i.count(element))) return i, unique_elements, duplicates my_str = 'fuzz' a,b,c = count_duplicates(my_str) print(a) print(b) print(c) [OUTPUT] fuzz # initial string {'f', 'u', 'z'} # set from string [('z', 2)] # dict of duplicates with count 

submitted by /u/Same-Criticism4335
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *