# Find the occurrence of numbers in a list.

What piece of code am I missing here, I can't figure it out.

Taking in a list of numbers, my function has to create a list of sets, for the unique number and occurrence.

With my currenct piece of code I get:
[(11.0, 0), (12.0, 0), (13.0, 0), (14.0, 0), (15.0, 0)]
with a random generated list for these numbers.

What do I need to create to put in the second part of each tuple(occurrence)?
``````def mode(x):
mySet=set(x)
myList=[]
count=0
for item in mySet:
newTuple=tuple([item,0])
myList.append(newTuple)
return myList
``````
###### Who is Participating?

Commented:
I've attached a solution:

``````def myfunc(x):
result = dict()
for i in x:
if i in result:
result[i] += 1
else:
result[i] = 1
result2 = []
for i in result:
result2.append((i,result[i]))
return result2

print myfunc([1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,4])

(prints:  [(1, 2), (2, 4), (3, 4), (4, 8)])
``````
0

Commented:
I am having a hard time figuring out what the object of your program is.  Can you give me an example of some sample input and the output you would like to see from it?
0

Author Commented:
I have a list that contains:
[11,11,11,11,12,12,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,15,15]
Using the function it should make a new list of tuples, for each tuple, the first value should be each unique number such as 11,12,13,14,15. The second part of each tuple should have the occurrence.
My output example should be something like:
[(11,4),(12,2),( 13,12),(14,4),(15,2)]

0

Software EngineerCommented:
I understand what you are trying to do, but I don't understand why you want/need to return a list of sets...
0

Author Commented:
Is there something similar in a list that would be better instead of sets?
0

Author Commented:
I will be using that list to create a table.
0

Commented:
Just for kicks, here's a more "Python-esque" solution with a list comprehension to clean up myfunc a bit:
``````def myfunc(x):
result = dict()
for i in x:
if i in result:
result[i] += 1
else:
result[i] = 1
result2 = []
return [(i, result[i]) for i in result]

print myfunc([1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,4])
``````
0

Commented:
Oops, you can eliminate the line that says "result2 = []" as well.
0

Author Commented:
What if, lets say the list is randomly generated.

How would I sort it in order with the function?
0

Commented:
By changing the last line of the function per attached code.

This will sort the list prior to returning it.  By default, Python will sort a list of tuples based on the first element in the tuple, which is exactly what you wish.

``````return [(i, result[i]) for i in result].sort()
``````
0

Author Commented:
``````def myfunc(x):
result = dict()
for i in x:
if i in result:
result[i] += 1
else:
result[i] = 1
return [(i, result[i]) for i in result].sort()
print myfunc([1,2,3,1,3,2,3,4])
``````
It returns "None"
0

Commented:
Sorry!  Try this.
``````return sorted([(i, result[i]) for i in result])
``````
0

Author Commented:
Thanks
0

Commented:
A bit shorter solution that uses a dictionary .get(key, default) method is:

``````def count(seq):
d = {}
for e in seq:
d[e] = d.get(e, 0) + 1
return [(k, d[k]) for k in d]

lst = [11,11,11,11,12,12,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,15,15]
print count(lst)
``````

For Python 2.7, there is a container called Counter (http://docs.python.org/library/collections.html#counter-objects) which does something similar:

``````import collections

lst = [11,11,11,11,12,12,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,15,15]
c = collections.Counter(lst)
print [(k, c[k]) for k in c]
``````

0

Commented:
Nice!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.