Having trouble getting pyhon to repeat things for me.

What I wanted to test was python's random.sample.
So say I have a list

myList = range(1, 15)

I can for a single time do this

import random
>>> myList = range(1,15)
>>> random.sample(myList, 3)
[10, 6, 11]
>>> 

I would like to be able to output
[10, 6, 11]
[1, 9, 3]
[8, 7, 2]
[1, 8, 11]
etc up to a statistic large range. Then count the occurrence of each item at its position in the list.

But I am looking at counter example from Python Collections

>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
...     cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})

But what I want to do is create sets out of random.sample(myList, 3) say 100 times and then check the count of the occurrences of items frequency.

So this is what I thought would work but list isn't a valid type.

>>> cnt = collections.Counter()
>>> for sample in range(100):
...     a = random.sample(myList, 3)
...     cnt[a] += 1
... 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
TypeError: unhashable type: 'list'

So I changed it up a little but still failed.

>>> cnt = collections.Counter()
>>> for sample in range(100):
...     a = random.sample(myList, 3)
...     cnt[0] [1] [2] += 1
... 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
TypeError: 'int' object has no attribute '__getitem__'
>>> 

You are trying to use a list as a dictionary key. Python list objects are mutable and can not be used as keys.

You could change them to tuples.

okay that has definiely got me going in the right direction

>>> for sample in range(100):
...     b = tuple(random.sample(a, 3))
...     cnt[b] += 1
... 
>>> cnt
Counter({(7, 3, 12): 2, (4, 10, 3): 2, (5, 10, 14): 1, (2, 7, 1): 1, (14, 11, 2): 1, (7, 1, 3): 1, (1, 9, 14): 1, (1, 7, 13): 1, (13, 7, 3): 1, (3, 5, 10): 1, (10, 9, 14): 1, (2, 14, 1): 1, (11, 3, 6): 1, (8, 6, 3): 1, (1, 5, 13): 1, (1, 11, 8): 1, (1, 10, 5): 1, (1, 5, 12): 1, (12, 2, 5): 1, (14, 12, 6): 1, (1, 13, 7): 1, (7, 4, 1): 1, (10, 1, 3): 1, (7, 13, 3): 1, (6, 10, 12): 1, (4, 1, 5): 1, (11, 9, 7): 1, (13, 4, 5): 1, (4, 2, 13): 1, (8, 6, 14): 1, (13, 5, 11): 1, (2, 8, 11): 1, (6, 5, 2): 1, (5, 8, 13): 1, (9, 14, 4): 1, (14, 10, 1): 1, (10, 2, 7): 1, (2, 5, 1): 1, (11, 1, 13): 1, (4, 6, 1): 1, (4, 7, 12): 1, (10, 4, 5): 1, (2, 5, 6): 1, (6, 7, 13): 1, (10, 1, 12): 1, (6, 13, 9): 1, (10, 1, 13): 1, (9, 7, 4): 1, (8, 10, 6): 1, (7, 5, 4): 1, (4, 9, 6): 1, (14, 1, 13): 1, (7, 11, 8): 1, (14, 4, 8): 1, (3, 5, 11): 1, (12, 14, 1): 1, (14, 7, 3): 1, (12, 7, 4): 1, (12, 4, 13): 1, (7, 12, 11): 1, (8, 4, 11): 1, (13, 5, 2): 1, (13, 11, 1): 1, (4, 1, 8): 1, (4, 14, 1): 1, (9, 6, 5): 1, (4, 14, 7): 1, (8, 10, 14): 1, (3, 14, 1): 1, (7, 5, 10): 1, (1, 9, 3): 1, (9, 4, 8): 1, (1, 9, 2): 1, (7, 10, 9): 1, (8, 4, 2): 1, (12, 7, 2): 1, (2, 14, 13): 1, (4, 8, 12): 1, (12, 6, 5): 1, (5, 6, 10): 1, (3, 4, 9): 1, (14, 2, 11): 1, (6, 2, 14): 1, (6, 5, 9): 1, (5, 6, 14): 1, (6, 11, 4): 1, (14, 5, 10): 1, (10, 11, 13): 1, (7, 5, 9): 1, (3, 14, 6): 1, (1, 6, 3): 1, (9, 13, 4): 1, (8, 14, 13): 1, (10, 5, 12): 1, (8, 4, 9): 1, (12, 11, 9): 1, (12, 5, 14): 1, (8, 14, 5): 1})
>>> 

I have effectively counted how many times each set(tuple) has occurred. If I want the sum of the occurrences of each number at each index of the tuples do I proceed from here to iterate and sum counter or have I misused counter?

>>> for tups in cnt:
...     for tupp in b:
...         sum(tups(tupp))
... 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.