+ 2
Set
I've wrote a code for integers occurring odd number of times. but if I use set , it's not printing in the order of elements occurring in the input. help https://code.sololearn.com/cI9vrNsdR7Np/?ref=app
3 Antworten
+ 4
Hi Loniie!
In both mathematics and Python, a set is an unordered collection of distinct elements. When we use sets, Python automatically assumes that the order doesn't matters to us, and uses its own favourite ordering (dictated by hash functions and whatnot). For your problem, if you want to find the distinct elements from c, but still preserve the order, you can do it manually like this:
p = []
for i in c:
if i not in p:
p.append(i)
Or using a special import:
from collections import OrderedDict
p = list(OrderedDict.fromkeys(c))
Hope that helps :)
+ 2
thanks :)
+ 2
You are welcome!
P.S. It's nothing new to you, but we can also bypass the list c and create p directly :)
p=[]
for i in a:
if a.count(i)%2 and i not in p:
p.append(i)