【发布时间】:2021-03-27 11:56:24
【问题描述】:
我有一个二维列表。内部列表都填充了 6 个介于 1 和 45 之间的随机整数。我想找出在 2 到 4 个数字之间的每个组合长度中,哪三个连续数字组合出现次数最多,以及它们实际出现的频率。为了简短起见,我只给出了出现次数最多的数字组合的例子,但我认为你明白了。我想到的清单和代码:
intlst = [[29, 38, 17, 30, 33, 41], [12, 20, 30, 33, 29, 38], [12, 20, 30, 29, 38, 41], [17, 30, 33, 41, 33, 45], [27, 29, 17, 30, 33, 41]]
所以两个数字长度的连续数字出现次数最多的组合是:29、38,出现了 3 次。
三个数字出现次数最多的组合是:12、20、30 出现两次。
出现次数最多的四个数字组合是:17、30、33、41 出现 3 次。
我想打印带有附加文本的结果,所以功能会很棒。这应该看起来像这样:
def countcombinations(intlst, length):
#do the math
return result
print("most occuring combinations with a length of two:",countcombinations(intlist, length),"\n most occuring combinations with a length of three:",countcombinations(intlist, length),"\n most occuring combinations with a length of four:",countcombinations(intlist, length))
所以输出看起来像这样:
most occuring combinations with a length of two: 29, 38 3x times
.., .. nx times
.., .. nx times
most occuring combinations with a length of three: 12, 20, 30 2x times
.., .., .. nx times
.., .., .. nx times
most occuring combinations with a length of four: 17, 30, 33, 41 3x times
.., .., .., .. nx times
.., .., .., .. nx times
我成功地使用元组获得了长度为 2 的结果,但我不知道如何用三个和四个数字长组合来做同样的事情。
【问题讨论】:
-
你的意思是同一子列表的元素组合?
-
不,实际上来自所有子列表。
-
包含预期结果的完整示例在哪里?你的代码呢?
标签: python python-3.x list numpy multidimensional-array