I have a list X with numbers from 1-10, and a function that uses this list to create a new list with random values from 1-10. I want to call this function 100 times and count the number of times the same value occurs on the same position in the nested list. I've created the function "lists" to achieve this. My code returns the correct answer, but I'm thinking there has to be a simpler way to write it.
from collections import Counter
import random
X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def randomX(l):
randomX = [random.choice(X) for i in range(len(data))]
return randomX
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))
This prints a result that looks like this, which is kind of the thing I want, except that I'm going to sort it. However, it seems unnecessary to write pretty much the same thing 10 times in a row, but list slicing won't work with counter.
(Counter({7: 14, 8: 12, 6: 12, 3: 11, 9: 11, 1: 10, 10: 9, 4: 9, 5: 7, 2: 5}),
Counter({5: 16, 3: 14, 7: 11, 4: 11, 10: 9, 1: 9, 6: 8, 8: 8, 2: 7, 9: 7}),
Counter({3: 14, 2: 14, 7: 13, 8: 13, 4: 10, 6: 10, 5: 10, 1: 8, 9: 5, 10: 3}),
Counter({3: 15, 6: 15, 8: 12, 7: 11, 1: 11, 4: 11, 2: 10, 9: 7, 5: 5, 10: 3}),
Counter({8: 20, 3: 15, 6: 13, 4: 11, 7: 10, 10: 10, 2: 7, 1: 7, 5: 4, 9: 3}),
Counter({9: 15, 6: 13, 10: 12, 4: 11, 2: 10, 8: 9, 3: 8, 5: 8, 1: 8, 7: 6}),
Counter({6: 17, 7: 13, 9: 11, 2: 11, 8: 11, 5: 10, 3: 8, 10: 8, 4: 6, 1: 5}),
Counter({6: 20, 5: 11, 10: 11, 1: 11, 9: 10, 2: 9, 4: 8, 3: 8, 7: 7, 8: 5}),
Counter({8: 13, 10: 13, 4: 13, 5: 11, 9: 11, 1: 10, 3: 9, 2: 8, 7: 6, 6: 6}),
Counter({10: 14, 8: 13, 1: 10, 2: 10, 3: 10, 5: 10, 7: 9, 4: 9, 9: 9, 6: 6}))
Does anyone have any suggestions as to how I can simplify this code?
Aucun commentaire:
Enregistrer un commentaire