[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

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

Open in new window

0
Enzo255
Asked:
Enzo255
1 Solution
 
-Richard-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
 
Enzo255Author 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
 
HonorGodCommented:
I understand what you are trying to do, but I don't understand why you want/need to return a list of sets...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Enzo255Author Commented:
Is there something similar in a list that would be better instead of sets?
0
 
Enzo255Author Commented:
I will be using that list to create a table.
0
 
-Richard-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)])

Open in new window

0
 
-Richard-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])

Open in new window

0
 
-Richard-Commented:
Oops, you can eliminate the line that says "result2 = []" as well.
0
 
Enzo255Author Commented:
What if, lets say the list is randomly generated.

How would I sort it in order with the function?
0
 
-Richard-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()

Open in new window

0
 
Enzo255Author 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])

Open in new window

It returns "None"
0
 
-Richard-Commented:
Sorry!  Try this.
return sorted([(i, result[i]) for i in result])

Open in new window

0
 
Enzo255Author Commented:
Thanks
0
 
peprCommented:
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)

Open in new window


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]

Open in new window


0
 
-Richard-Commented:
Nice!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now