Jim Larson
Jim Larson

Reputation: 55

I need to get my main function working

I am trying to use my create_list to generate 10,000 numbers between 1-6. The count_list is suppose to count the amount of 1,2,3,4,5,6's that were generated and the main function is suppose to attach the two and then prints the results of each. BTW I just started using def so go easy on me ;)

def count_list(my_list, c):
     amount_one=0
     amount_two=0
     amount_three=0
     amount_four=0
     amount_five=0
     amount_six=0
     or number in my_list:
         if number==1:
             amount_one+=1
         elif number==2:
             amount_two+=1
         elif number==3:
             amount_three+=1
         elif number==4:
             amount_four+=1
         elif number==5:
             amount_five+=1
         elif number==6:
             amount_six+=1
     return amount_one
     return amount_two
     return amount_three
     return amount_four
     return amount_five
     return amount_six
def create_list(n):
     import random
     my_list=[]
     for i in range(10000):
         n=random.randint(1,6)
         my_list.append(n)
     return my_list
def main():
     hello=count_list(create_list)
     print "1-",count_list()[0]
     print "2-",count_list()[1]
     print "3-",count_list()[2]
     print "4-",count_list()[3]
     print "5-",count_list()[4]
     print "6-",count_list()[5]

 main()

Upvotes: 0

Views: 108

Answers (3)

Cory Kramer
Cory Kramer

Reputation: 117886

You can use the argument n to determine the number of numbers to generate in your list

def create_list(n):
    import random
    my_list = []
    for i in range(n):
        my_list.append(random.randint(1,6))
    return my_list

To count the items in your list, a straight forward way to do it would be like so

def count_list(l):
    c = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0}  # initialize dictionary
    for key in c:
        c[key] = l.count(key)
    return c

Now we can call the above functions in main

def main():
    nums = create_list(1000)
    counts = count_list(nums)
    for i in range(1,7):
        print(i, counts[i])

Testing

>>> main()
1 185
2 166
3 162
4 168
5 143
6 176

For fun, here are some slightly more advanced methods to do the same thing in a more efficient way

def create_list(n):
    import random
    return [random.randint(1,6) for _ in range(n)]  # using a list comprehension

def count_list(l):
    from collections import Counter  # using the Counter class
    return Counter(l)

def main():
    nums = create_list(1000)
    counts = count_list(nums)
    for i in range(1,7):
        print(i, counts[i])

Upvotes: 3

nishparadox
nishparadox

Reputation: 770

You can use a dictionary to map the count with corresponding number:

def count_list(my_list):
    from collections import defaultdict
    dict = defaultdict(int)

    for x in my_list:
        dict[x] += 1

    return dict


def create_list(a,b):
     import random
     my_list=[]
     for i in range(10000):
         n=random.randint(a,b)
         my_list.append(n)
     return my_list

def main():
     lst = create_list(1,6)
     dict = count_list(lst)
     print(dict)


if __name__=="__main__":
    main()

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

use collections.Counter to do the counting, just pass it your list of numbers returned from create_list:

from collections import Counter
import random


def create_list():
    my_list = []
    for i in range(10000):
        n = random.randint(1, 6)
        my_list.append(n)
    return my_list


print(Counter(create_list()))

Counter({1: 1702, 4: 1695, 6: 1692, 3: 1688, 5: 1651, 2: 1572})

If you want the 3 most common numbers etc.. use .most_common(3):

print(Counter(create_list()).most_common(3))

Upvotes: 2

Related Questions