[英]Find most frequent combinations of numbers in 2D list python
I have a two dimensional list.我有一个二维列表。 The inner lists are all filled with 6 random Integers between 1 and 45. I want to find out which three combinations of consecutive numbers are the most occurring for each combination length between two and four numbers and how often they actually occure.
内部列表都填充了 1 到 45 之间的 6 个随机整数。我想找出在 2 到 4 个数字之间的每个组合长度中,哪三个连续数字组合出现次数最多,以及它们实际出现的频率。 To keep it short I gave the example with only the most occuring number combinations but I think you get the point.
为了简短起见,我只给出了出现次数最多的数字组合的例子,但我认为你明白了。 My list and code I thought about:
我想到的清单和代码:
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]]
So the most occuring combination of consecutive numbers with a length of two numbers is: 29, 38 which occures three times.因此,长度为两个数字的连续数字出现次数最多的组合是:29、38,它们出现了 3 次。
The most occuring combination with three numbers is: 12, 20, 30 occuring two times.三个数字出现次数最多的组合是:12、20、30 出现两次。
The most occuring combination with four numbers is: 17, 30, 33, 41 occuring three times.四个数字出现最多的组合是:17、30、33、41 出现 3 次。
I want to print a result with additional text so a function would be great.我想打印带有附加文本的结果,因此 function 会很棒。 This should look something like this:
这应该看起来像这样:
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))
So the output would look something like this:所以 output 看起来像这样:
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
I was successful to get the result with a length of two using tuples but I don´t know how to do the same thing with three and four numbers long combinations.我成功地使用元组获得了长度为 2 的结果,但我不知道如何使用三个和四个数字长组合来做同样的事情。
itertools.combinations is the best tool for this task. itertools.combinations 是完成这项任务的最佳工具。 See below:
见下文:
from itertools import combinations
from collections import Counter
def most_freq(l, n):
temp=[]
for i in l:
temp.extend(tuple(k) for k in combinations(i, n))
res = Counter(temp)
return res
for i in range(2,5):
print(most_freq(l, i))
Output for n=2: Output 对于 n = 2:
l=[[2, 13, 30, 33, 39, 41], [17, 20, 27, 33, 39, 38]]
>>>print(most_freq(l, i))
Counter({(33, 39): 2, (2, 13): 1, (2, 30): 1, (2, 33): 1, (2, 39): 1, (2, 41): 1, (13, 30): 1, (13, 33): 1, (13, 39): 1, (13, 41): 1, (30, 33): 1, (30, 39): 1, (30, 41): 1, (33, 41): 1, (39, 41): 1, (17, 20): 1, (17, 27): 1, (17, 33): 1, (17, 39): 1, (17, 38): 1, (20, 27): 1, (20, 33): 1, (20, 39): 1, (20, 38): 1, (27, 33): 1, (27, 39): 1, (27, 38): 1, (33, 38): 1, (39, 38): 1})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.