Source From Here
Question
If I have a list of tuple as below:
How to count pair of number such as:
Then we got: {(1, 2): 2, (1, 3): 1, (2, 3): 2, (3, 4): 1, (2, 4): 1}
How-To
What you want is to count pairs that arise from combinations in your lists. You can find those with a Counter and combinations.
Output:
Question
If I have a list of tuple as below:
- datas = [(1, 2), (1, 2, 3), (2, 3, 4)]
Then we got: {(1, 2): 2, (1, 3): 1, (2, 3): 2, (3, 4): 1, (2, 4): 1}
How-To
What you want is to count pairs that arise from combinations in your lists. You can find those with a Counter and combinations.
- from itertools import combinations
- from collections import Counter
- datas = [(1, 2), (1, 2, 3), (2, 3, 4)]
- counter = Counter()
- for t in datas:
- counter.update(Counter(combinations(t, 2)))
- display(counter)
沒有留言:
張貼留言