Вы можете переместить созданный список в столбцы, используя zip () и сосчитать их.
Небольшой пример того, что zip () делает:
d = [ [1,2,3,4], [11,12,13,14], [111,112,113,114]]
print(list(zip(*d)))
Выход:
# transposed - each tuple equals one column now
[(1, 11, 111), (2, 12, 112), (3, 13, 113), (4, 14, 114)]
Ваш код с использованием zip () и генератора:
from collections import Counter
import random
random.seed(42)
def countEm(data, times):
datalen = len(data)
numbers = [ random.choices(data, k=datalen) for _ in range(times)]
for col in zip(*numbers):
yield Counter(col)
X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# generators can only be consumed once, use result = list(countEm(X,100)) if you
# want to reuse the result beside printing...
for count in countEm(X,100):
print(count)
Выход:
Counter({9: 18, 7: 13, 10: 13, 5: 12, 3: 11, 8: 11, 1: 8, 4: 6, 6: 4, 2: 4})
Counter({3: 16, 7: 14, 6: 13, 8: 11, 10: 10, 1: 9, 4: 9, 5: 8, 9: 6, 2: 4})
Counter({6: 13, 7: 13, 3: 12, 9: 12, 2: 11, 5: 10, 10: 9, 8: 8, 4: 7, 1: 5})
Counter({10: 17, 2: 15, 6: 12, 3: 9, 9: 9, 5: 9, 8: 9, 7: 8, 1: 7, 4: 5})
Counter({9: 14, 10: 11, 4: 11, 7: 10, 1: 10, 5: 10, 3: 9, 6: 9, 8: 8, 2: 8})
Counter({7: 17, 3: 12, 8: 11, 9: 11, 10: 10, 4: 9, 2: 9, 5: 8, 1: 7, 6: 6})
Counter({3: 13, 2: 13, 5: 13, 1: 11, 10: 11, 6: 10, 8: 9, 7: 8, 9: 7, 4: 5})
Counter({6: 15, 3: 13, 5: 13, 1: 12, 10: 12, 7: 11, 8: 8, 4: 7, 9: 5, 2: 4})
Counter({5: 15, 1: 12, 8: 12, 7: 10, 2: 10, 6: 10, 9: 9, 3: 9, 4: 8, 10: 5})
Counter({3: 13, 9: 13, 4: 12, 10: 10, 2: 10, 1: 9, 8: 9, 7: 8, 6: 8, 5: 8})
Примечания:
Наши случайные числа не будут совпадать, даже если вы используете тот же random.seed(42)
. Использование n раз random.choice()
изменяет внутреннее случайное Mersenne_Twister -состояние, отличающееся от использования random.choices(.., k=n)
- если вы переключились на random.choices
, вы получите идентичные выходные данные:
from collections import Counter
import random
random.seed(42)
X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def randomX(l):
lenl=len(l)
return random.choices(l,k=lenl)
def lists(times):
lists = [randomX(X) for i in range(times)]
x1 = Counter(sublist[0] for sublist in lists)
x2 = Counter(sublist[1] for sublist in lists)
x3 = Counter(sublist[2] for sublist in lists)
x4 = Counter(sublist[3] for sublist in lists)
x5 = Counter(sublist[4] for sublist in lists)
x6 = Counter(sublist[5] for sublist in lists)
x7 = Counter(sublist[6] for sublist in lists)
x8 = Counter(sublist[7] for sublist in lists)
x9 = Counter(sublist[8] for sublist in lists)
x10 = Counter(sublist[9] for sublist in lists)
return x1, x2, x3, x4, x5, x6, x7, x8, x9, x10
print(*lists(100), sep="\n")
Вывод сейчас:
Counter({9: 18, 7: 13, 10: 13, 5: 12, 3: 11, 8: 11, 1: 8, 4: 6, 6: 4, 2: 4})
Counter({3: 16, 7: 14, 6: 13, 8: 11, 10: 10, 1: 9, 4: 9, 5: 8, 9: 6, 2: 4})
Counter({6: 13, 7: 13, 3: 12, 9: 12, 2: 11, 5: 10, 10: 9, 8: 8, 4: 7, 1: 5})
Counter({10: 17, 2: 15, 6: 12, 3: 9, 9: 9, 5: 9, 8: 9, 7: 8, 1: 7, 4: 5})
Counter({9: 14, 10: 11, 4: 11, 7: 10, 1: 10, 5: 10, 3: 9, 6: 9, 8: 8, 2: 8})
Counter({7: 17, 3: 12, 8: 11, 9: 11, 10: 10, 4: 9, 2: 9, 5: 8, 1: 7, 6: 6})
Counter({3: 13, 2: 13, 5: 13, 1: 11, 10: 11, 6: 10, 8: 9, 7: 8, 9: 7, 4: 5})
Counter({6: 15, 3: 13, 5: 13, 1: 12, 10: 12, 7: 11, 8: 8, 4: 7, 9: 5, 2: 4})
Counter({5: 15, 1: 12, 8: 12, 7: 10, 2: 10, 6: 10, 9: 9, 3: 9, 4: 8, 10: 5})
Counter({3: 13, 9: 13, 4: 12, 10: 10, 2: 10, 1: 9, 8: 9, 7: 8, 6: 8, 5: 8})