计算嵌套列表中每个位置的出现次数

时间:2018-11-02 13:49:32

标签: python python-3.x list random counter

我有一个列表X,其中的数字来自1-10,还有一个函数使用该列表创建一个新列表,其随机值的范围为1-10。我想调用此函数100次,并计算在嵌套列表中相同位置的相同值发生的次数。

我已经创建函数lists来实现此目的。我的代码返回了正确的答案,但我认为必须采用一种更简单的方式编写它:

从馆藏进口柜台     随机导入

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))

这将打印出一个看起来像这样的结果,这是我想要的东西,除了我要对其进行排序。但是,似乎不必连续10次编写几乎相同的内容,但是列表切片对计数器不起作用。

(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}))

有人对我如何简化此代码有任何建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用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()更改内部random Mersenne_Twister状态与使用random.choices(.., k=n)有所不同-如果切换到random.choices,您将获得相同的输出:< / p>

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})