Find duplicates in a 3 Dimensional tuple list - python
At first, I apologize because I am a newbie...
I have one 3d list of tuple:
list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
I want to find duplicates and display like this, no matter the position of numbers ,only to have the same numbers :
{ (0,1,6): 2,
(4,5,1): 2,
(3,2,1): 1 }
I want to count the similar tuples.
Any suggestions?
You say you are initializing your list with:
mylist = [[[(x,y,z) for x in range(7)] for y in range(7)] for z in range(7)]
At this point, you can change how you initialize it:
from collections import Counter
# take off some square brackets
mylist = [(x,y,z) for x in range(7) for y in range(7) for z in range(7)]
dict(Counter(tuple(sorted(tup)) for tup in mylist))
Output:
{(0, 0, 0): 1, (0, 0, 1): 3, (0, 0, 2): 3, (0, 0, 3): 3, (0, 0, 4): 3, (0, 0, 5): 3, (0, 0, 6): 3, (0, 1, 1): 3, (0, 1, 2): 6, (0, 1, 3): 6, (0, 1, 4): 6, (0, 1, 5): 6, (0, 1, 6): 6, (0, 2, 2): 3, (0, 2, 3): 6, (0, 2, 4): 6, (0, 2, 5): 6, (0, 2, 6): 6, (0, 3, 3): 3, (0, 3, 4): 6, (0, 3, 5): 6, (0, 3, 6): 6, (0, 4, 4): 3, (0, 4, 5): 6, (0, 4, 6): 6, (0, 5, 5): 3, (0, 5, 6): 6, (0, 6, 6): 3, (1, 1, 1): 1, (1, 1, 2): 3, (1, 1, 3): 3, (1, 1, 4): 3, (1, 1, 5): 3, (1, 1, 6): 3, (1, 2, 2): 3, (1, 2, 3): 6, (1, 2, 4): 6, (1, 2, 5): 6, (1, 2, 6): 6, (1, 3, 3): 3, (1, 3, 4): 6, (1, 3, 5): 6, (1, 3, 6): 6, (1, 4, 4): 3, (1, 4, 5): 6, (1, 4, 6): 6, (1, 5, 5): 3, (1, 5, 6): 6, (1, 6, 6): 3, (2, 2, 2): 1, (2, 2, 3): 3, (2, 2, 4): 3, (2, 2, 5): 3, (2, 2, 6): 3, (2, 3, 3): 3, (2, 3, 4): 6, (2, 3, 5): 6, (2, 3, 6): 6, (2, 4, 4): 3, (2, 4, 5): 6, (2, 4, 6): 6, (2, 5, 5): 3, (2, 5, 6): 6, (2, 6, 6): 3, (3, 3, 3): 1, (3, 3, 4): 3, (3, 3, 5): 3, (3, 3, 6): 3, (3, 4, 4): 3, (3, 4, 5): 6, (3, 4, 6): 6, (3, 5, 5): 3, (3, 5, 6): 6, (3, 6, 6): 3, (4, 4, 4): 1, (4, 4, 5): 3, (4, 4, 6): 3, (4, 5, 5): 3, (4, 5, 6): 6, (4, 6, 6): 3, (5, 5, 5): 1, (5, 5, 6): 3, (5, 6, 6): 3, (6, 6, 6): 1}
If you don't want to change how you initialized it:
def flatten(alist):
for item in alist:
if isinstance(item, list):
yield from flatten(item)
else:
yield item
dict(Counter(tuple(sorted(tup)) for tup in flatten(mylist)))
list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
#first please make a list of sorted tuples to ignore the sequence of numbers
sorted_list = [sorted(x) for x in list]
#then count the tuple in sorted list and ignore count for duplicated items.
list = [ { x: sorted_list.count(sorted(x)) } for i, x in enumerate(list) if sorted(x) not in sorted_list[:i]]
print(list);
import pandas as pd
import numpy as np
# make a dataframe out of the list
df = pd.DataFrame(data=ll)
# sort columns and find duplicates
df_dup = df[pd.DataFrame(np.sort(df.values), columns=df.columns, index=df.index)
.duplicated(keep=False)]
# return duplicates as dictionary of tuples
result = df_dup.T.apply(tuple).to_dict()
# return number of duplicates found
n = len(result)
Use Counter from collections:
>>> from collections import Counter
>>> list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
>>> dict(Counter(map(lambda x: tuple(sorted(x)),list)))
{(0, 1, 6): 2, (1, 4, 5): 2, (1, 2, 3): 1}
>>>
Or a-style-of list comprehension:
>>> from collections import Counter
>>> list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
>>> dict(Counter([tuple(sorted(i)) for i in list]))
{(0, 1, 6): 2, (1, 4, 5): 2, (1, 2, 3): 1}
>>>
Also, list is a replica of a python keyword, after making that a variable, you would't have any access to the keyword anymore, so it's better to rename that variable, and so rename it in my code.
Edit:
Use:
dict(Counter([tuple(sorted(x)) for i in list for x in i]))
as suggestion, not to use keyword in python as a variable, here is the keywords in python:
import keyword
keyword_list = keyword.kwlist
for your question, here is my answer :
from collections import Counter
result = dict(Counter([tuple(sorted(item)) for item in your_list ]))
Related
Find all permutations of numbers of exactly 9 characters long
I have the numbers [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16]. I want to find all permutations of numbers of exactly 9 characters. For example, if I have 8 single digit numbers, the last one can't be a double digit, because it would exceed the character limit. Clarifications: The same number can be reused, so for example the number 1 nine times is valid. There can be any number of double digit numbers, as long as the total character length of the digits is exactly 9. For example, 1, 10, 12, 15, 16 is valid. I tried itertools.permutations, but I couldn't get it to work with two-digit numbers.
I would use a mixture of combination and permutations. First find all of the combinations of the data which add up to the desired length. Then for each unique combination, find their permutations. There's probably some work to be done here which can limit the amount of incorrect combinations checked: import itertools def perm_of_length(data, length): for i in range(len(data)): for comb in itertools.combinations(data, i + 1): if sum(map(len, (map(str, comb)))) == length: for perm in itertools.permutations(comb): yield perm for perm in perm_of_length([0, 1, 2, 3, 10, 12], 4): print(perm) Outputs: (10, 12) (12, 10) (0, 1, 10) (0, 10, 1) (1, 0, 10) (1, 10, 0) (10, 0, 1) (10, 1, 0) (0, 1, 12) (0, 12, 1) (1, 0, 12) (1, 12, 0) (12, 0, 1) (12, 1, 0) (0, 2, 10) (0, 10, 2) (2, 0, 10) (2, 10, 0) (10, 0, 2) (10, 2, 0) (0, 2, 12) (0, 12, 2) (2, 0, 12) (2, 12, 0) (12, 0, 2) (12, 2, 0) (0, 3, 10) (0, 10, 3) (3, 0, 10) (3, 10, 0) (10, 0, 3) (10, 3, 0) (0, 3, 12) (0, 12, 3) (3, 0, 12) (3, 12, 0) (12, 0, 3) (12, 3, 0) (1, 2, 10) (1, 10, 2) (2, 1, 10) (2, 10, 1) (10, 1, 2) (10, 2, 1) (1, 2, 12) (1, 12, 2) (2, 1, 12) (2, 12, 1) (12, 1, 2) (12, 2, 1) (1, 3, 10) (1, 10, 3) (3, 1, 10) (3, 10, 1) (10, 1, 3) (10, 3, 1) (1, 3, 12) (1, 12, 3) (3, 1, 12) (3, 12, 1) (12, 1, 3) (12, 3, 1) (2, 3, 10) (2, 10, 3) (3, 2, 10) (3, 10, 2) (10, 2, 3) (10, 3, 2) (2, 3, 12) (2, 12, 3) (3, 2, 12) (3, 12, 2) (12, 2, 3) (12, 3, 2) (0, 1, 2, 3) (0, 1, 3, 2) (0, 2, 1, 3) (0, 2, 3, 1) (0, 3, 1, 2) (0, 3, 2, 1) (1, 0, 2, 3) (1, 0, 3, 2) (1, 2, 0, 3) (1, 2, 3, 0) (1, 3, 0, 2) (1, 3, 2, 0) (2, 0, 1, 3) (2, 0, 3, 1) (2, 1, 0, 3) (2, 1, 3, 0) (2, 3, 0, 1) (2, 3, 1, 0) (3, 0, 1, 2) (3, 0, 2, 1) (3, 1, 0, 2) (3, 1, 2, 0) (3, 2, 0, 1) (3, 2, 1, 0) Proof this works: for perm in perm_of_length([0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16], 9): assert sum(map(len, map(str, perm))) == 9
NOTE: this is much slower than it needs to be. A simple, non-performant, brute-force approach with filtering: symbols = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16] symbols = [str(x) for x in symbols] perms = itertools.chain.from_iterable( itertools.permutations(symbols, i) for i in range(10) ) perms = ("".join(x) for x in perms) perms = [x for x in perms if len(x) <= 9] >>> len(perms) 13600046 >>> perms[:4] ['', '0', '1', '2'] >>> perms[-4:] ['987643102', '987643120', '987643201', '987643210'] One can drop the empty string by simply skipping the first item in perms. A similar approach which does not use strings: symbols = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16] lengths = {k: int(math.log10(k)) + 1 if k != 0 else 1 for k in symbols} perms = itertools.chain.from_iterable( itertools.permutations(symbols, i) for i in range(10) ) perms = [x for x in perms if sum(lengths[k] for k in x) <= 9] >>> len(perms) 13600046 If numbers can be reused (as mentioned in the comments), I would consider itertools.product instead of itertools.permutations: symbols = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16] lengths = {k: int(math.log10(k)) + 1 if k != 0 else 1 for k in symbols} perms = itertools.product(symbols, repeat=9) perms = [x for x in perms if sum(lengths[k] for k in x) <= 9]
Listing all combination of list 1 to n given length restriction k
I found a code that recursively returns all combination of a list of 1 to n numbers with length k def choose_iter(elements, length): for i in range(len(elements)): if length == 1: yield (elements[i],) else: for next in choose_iter(elements[i+1:len(elements)], length-1): yield (elements[i],) + next def choose(l, k): return list(choose_iter(l, k)) this will return what I need but can I modify this so that I dont have to use the yield function? I haven't studied yield yet and I don't want to confuse myself with this.
You can do it using itertools: import itertools for combination in itertools.combinations([i for i in range(1, n+1)], k): print(combination) For n=7 and k=5, you have: (1, 2, 3, 4, 5) (1, 2, 3, 4, 6) (1, 2, 3, 4, 7) (1, 2, 3, 5, 6) (1, 2, 3, 5, 7) (1, 2, 3, 6, 7) (1, 2, 4, 5, 6) (1, 2, 4, 5, 7) (1, 2, 4, 6, 7) (1, 2, 5, 6, 7) (1, 3, 4, 5, 6) (1, 3, 4, 5, 7) (1, 3, 4, 6, 7) (1, 3, 5, 6, 7) (1, 4, 5, 6, 7) (2, 3, 4, 5, 6) (2, 3, 4, 5, 7) (2, 3, 4, 6, 7) (2, 3, 5, 6, 7) (2, 4, 5, 6, 7) (3, 4, 5, 6, 7)
I think this should be the correct code def choose_iter(elements, length): for i in range(len(elements)): if length == 1: yield (elements[i],) else: for j in choose_iter(elements[:len(elements)], length - 1): yield (elements[i],) + j def choose(l, k): for i in choose_iter(l, k): print(i) yield is similar to return but in this case, you need the yield statement otherwise the code will not work. You can check What does the "yield" keyword do? for more info. And also avoid using "next" as a variable because it's also a python function. Hope I helped.
Permutations Function Issue
I've been struggling with defining a function that returns the permutations of a given array. I have written the code below: def gPermutations(array): result = [] idx = 0 for element in array: for i in range(len(array)): if i != idx: z = array[:array.index(element)] z.append(array[i]) s = z[:] + array[array.index(element)+1:] s[i] = element result.append(s) idx += 1 return result This code seems to work by returning some of the permutations of an array, but doesn't return all of them, and sometimes duplicates a certain permutation. May someone please explain what is the issue with my code? Thanks!
Use the permutations method from itertools In [1]: from itertools import permutations In [2]: arr = [1,2,3,4] In [3]: for perm in permutations(arr, len(arr)): ...: print(perm) ...: (1, 2, 3, 4) (1, 2, 4, 3) (1, 3, 2, 4) (1, 3, 4, 2) (1, 4, 2, 3) (1, 4, 3, 2) (2, 1, 3, 4) (2, 1, 4, 3) (2, 3, 1, 4) (2, 3, 4, 1) (2, 4, 1, 3) (2, 4, 3, 1) (3, 1, 2, 4) (3, 1, 4, 2) (3, 2, 1, 4) (3, 2, 4, 1) (3, 4, 1, 2) (3, 4, 2, 1) (4, 1, 2, 3) (4, 1, 3, 2) (4, 2, 1, 3) (4, 2, 3, 1) (4, 3, 1, 2) (4, 3, 2, 1)
If you truly want all the permutations (re-orderings?) of your list, I'd use the itertools package: >>> from itertools import permutations >>> array = [1, 2, 3] >>> print(list(permutations(array))) [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
print pandas dataframe in pygame line by line
I am trying to print my dataframe in pygame's window. I know it is not a really good thing to do, but I have to in order to let my data be seen and avoid normal print. I have this dictionary: Y = {a: dict(b) for a, b in X.items()} print(Y) the result is: {(0, 0): {(0, 0): 0, (0, 1): 1, (0, 2): 2, (0, 3): 3, (1, 3): 4, (0, 4): 4, (0, 5): 7, (1, 5): 6, (1, 0): 9, (2, 0): 8, (1, 1): 10, (1, 2): 5, (2, 2): 6, (1, 4): 5, (2, 1): 7, (2, 3): 7, (2, 4): 8, (2, 5): 9}, (0, 1): {(0, 1): 0, (0, 0): 1, (0, 2): 1, (0, 3): 2, (1, 3): 3, (0, 4): 3, (0, 5): 6, (1, 5): 5, (1, 0): 8, (2, 0): 7, (1, 1): 9, (1, 2): 4, (2, 2): 5, (1, 4): 4, (2, 1): 6, (2, 3): 6, (2, 4): 7, (2, 5): 8}, (0, 2): {(0, 2): 0, (0, 1): 1, (0, 3): 1, (0, 0): 2, (1, 3): 2, (0, 4): 2, (0, 5): 5, (1, 5): 4, (1, 0): 7, (2, 0): 6, (1, 1): 8, (1, 2): 3, (2, 2): 4, (1, 4): 3, (2, 1): 5, (2, 3): 5, (2, 4): 6, (2, 5): 7}, (0, 3): {(0, 3): 0, (0, 2): 1, (1, 3): 1, (0, 4): 1, (0, 0): 3, (0, 1): 2, (0, 5): 4, (1, 5): 3, (1, 0): 6, (2, 0): 5, (1, 1): 7, (1, 2): 2, (2, 2): 3, (1, 4): 2, (2, 1): 4, (2, 3): 4, (2, 4): 5, (2, 5): 6}, (1, 3): {(1, 3): 0, (0, 3): 1, (1, 2): 1, (1, 4): 1, (0, 0): 4, (0, 1): 3, (0, 2): 2, (0, 4): 2, (0, 5): 3, (1, 5): 2, (1, 0): 5, (2, 0): 4, (1, 1): 6, (2, 2): 2, (2, 1): 3, (2, 3): 3, (2, 4): 4, (2, 5): 5}, (0, 4): {(0, 4): 0, (0, 3): 1, (0, 0): 4, (0, 1): 3, (0, 2): 2, (1, 3): 2, (0, 5): 5, (1, 5): 4, (1, 0): 7, (2, 0): 6, (1, 1): 8, (1, 2): 3, (2, 2): 4, (1, 4): 3, (2, 1): 5, (2, 3): 5, (2, 4): 6, (2, 5): 7}, (0, 5): {(0, 5): 0, (1, 5): 1, (0, 0): 7, (0, 1): 6, (0, 2): 5, (0, 3): 4, (1, 3): 3, (0, 4): 5, (1, 0): 8, (2, 0): 7, (1, 1): 9, (1, 2): 4, (2, 2): 5, (1, 4): 2, (2, 1): 6, (2, 3): 6, (2, 4): 7, (2, 5): 8}, (1, 5): {(1, 5): 0, (0, 5): 1, (1, 4): 1, (0, 0): 6, (0, 1): 5, (0, 2): 4, (0, 3): 3, (1, 3): 2, (0, 4): 4, (1, 0): 7, (2, 0): 6, (1, 1): 8, (1, 2): 3, (2, 2): 4, (2, 1): 5, (2, 3): 5, (2, 4): 6, (2, 5): 7}, (1, 0): {(1, 0): 0, (2, 0): 1, (1, 1): 1, (0, 0): 9, (0, 1): 8, (0, 2): 7, (0, 3): 6, (1, 3): 5, (0, 4): 7, (0, 5): 8, (1, 5): 7, (1, 2): 4, (2, 2): 3, (1, 4): 6, (2, 1): 2, (2, 3): 4, (2, 4): 5, (2, 5): 6}, (2, 0): {(2, 0): 0, (1, 0): 1, (2, 1): 1, (0, 0): 8, (0, 1): 7, (0, 2): 6, (0, 3): 5, (1, 3): 4, (0, 4): 6, (0, 5): 7, (1, 5): 6, (1, 1): 2, (1, 2): 3, (2, 2): 2, (1, 4): 5, (2, 3): 3, (2, 4): 4, (2, 5): 5}, (1, 1): {(1, 1): 0, (1, 0): 1, (0, 0): 10, (0, 1): 9, (0, 2): 8, (0, 3): 7, (1, 3): 6, (0, 4): 8, (0, 5): 9, (1, 5): 8, (2, 0): 2, (1, 2): 5, (2, 2): 4, (1, 4): 7, (2, 1): 3, (2, 3): 5, (2, 4): 6, (2, 5): 7}, (1, 2): {(1, 2): 0, (1, 3): 1, (2, 2): 1, (0, 0): 5, (0, 1): 4, (0, 2): 3, (0, 3): 2, (0, 4): 3, (0, 5): 4, (1, 5): 3, (1, 0): 4, (2, 0): 3, (1, 1): 5, (1, 4): 2, (2, 1): 2, (2, 3): 2, (2, 4): 3, (2, 5): 4}, (2, 2): {(2, 2): 0, (1, 2): 1, (2, 1): 1, (2, 3): 1, (0, 0): 6, (0, 1): 5, (0, 2): 4, (0, 3): 3, (1, 3): 2, (0, 4): 4, (0, 5): 5, (1, 5): 4, (1, 0): 3, (2, 0): 2, (1, 1): 4, (1, 4): 3, (2, 4): 2, (2, 5): 3}, (1, 4): {(1, 4): 0, (1, 3): 1, (1, 5): 1, (0, 0): 5, (0, 1): 4, (0, 2): 3, (0, 3): 2, (0, 4): 3, (0, 5): 2, (1, 0): 6, (2, 0): 5, (1, 1): 7, (1, 2): 2, (2, 2): 3, (2, 1): 4, (2, 3): 4, (2, 4): 5, (2, 5): 6}, (2, 1): {(2, 1): 0, (2, 0): 1, (2, 2): 1, (0, 0): 7, (0, 1): 6, (0, 2): 5, (0, 3): 4, (1, 3): 3, (0, 4): 5, (0, 5): 6, (1, 5): 5, (1, 0): 2, (1, 1): 3, (1, 2): 2, (1, 4): 4, (2, 3): 2, (2, 4): 3, (2, 5): 4}, (2, 3): {(2, 3): 0, (2, 2): 1, (2, 4): 1, (0, 0): 7, (0, 1): 6, (0, 2): 5, (0, 3): 4, (1, 3): 3, (0, 4): 5, (0, 5): 6, (1, 5): 5, (1, 0): 4, (2, 0): 3, (1, 1): 5, (1, 2): 2, (1, 4): 4, (2, 1): 2, (2, 5): 2}, (2, 4): {(2, 4): 0, (2, 3): 1, (2, 5): 1, (0, 0): 8, (0, 1): 7, (0, 2): 6, (0, 3): 5, (1, 3): 4, (0, 4): 6, (0, 5): 7, (1, 5): 6, (1, 0): 5, (2, 0): 4, (1, 1): 6, (1, 2): 3, (2, 2): 2, (1, 4): 5, (2, 1): 3}, (2, 5): {(2, 5): 0, (2, 4): 1, (0, 0): 9, (0, 1): 8, (0, 2): 7, (0, 3): 6, (1, 3): 5, (0, 4): 7, (0, 5): 8, (1, 5): 7, (1, 0): 6, (2, 0): 5, (1, 1): 7, (1, 2): 4, (2, 2): 3, (1, 4): 6, (2, 1): 4, (2, 3): 2}}[! Then, I can save it into a dataframe: df = pd.DataFrame(Y) df.index = [*df.index] df.columns = [*df.columns] print(df) and the result is: (0, 0) (0, 1) (0, 2) (0, 3) ... (2, 1) (2, 3) (2, 4) (2, 5) (0, 0) 0 1 2 3 ... 7 7 8 9 (0, 1) 1 0 1 2 ... 6 6 7 8 (0, 2) 2 1 0 1 ... 5 5 6 7 (0, 3) 3 2 1 0 ... 4 4 5 6 (1, 3) 4 3 2 1 ... 3 3 4 5 (0, 4) 4 3 2 1 ... 5 5 6 7 (0, 5) 7 6 5 4 ... 6 6 7 8 (1, 5) 6 5 4 3 ... 5 5 6 7 (1, 0) 9 8 7 6 ... 2 4 5 6 (2, 0) 8 7 6 5 ... 1 3 4 5 (1, 1) 10 9 8 7 ... 3 5 6 7 (1, 2) 5 4 3 2 ... 2 2 3 4 (2, 2) 6 5 4 3 ... 1 1 2 3 (1, 4) 5 4 3 2 ... 4 4 5 6 (2, 1) 7 6 5 4 ... 0 2 3 4 (2, 3) 7 6 5 4 ... 2 0 1 2 (2, 4) 8 7 6 5 ... 3 1 0 1 (2, 5) 9 8 7 6 ... 4 2 1 0 My question is: Is it possible to print this last result on pygame? My idea is to save every line into an array and then print every line of the array alone and in the right order. I did it in this way: def printAM(x, y, cella, df): arraydf = df.to_numpy() y_offset = 0 for line in arraydf: textsurface = myfontsmall.render(str(line), False, (255, 255, 255)) screen.blit(textsurface, (x, y+y_offset)) y_offset += cella pygame.display.update() But the result is not really good, as you can see from this image of the result. How can I make it mode goodlooking? And do you think it is possible to have also the nodes' names as first line and as last column? Thanks
get the count of elements of tuples of your own...not just the range or sequence
The below code is running for first three elements of the tuple of this list SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] from collections import Counter c = Counter(elem[0:3] for elem in SS1) for k, v in c.items(): if (v > 0): print(k,v) and the output is: (1, 2, 3) 3 (1, 2, 4) 1 (1, 3, 4) 1 (2, 3, 4) 1 But my expectation is not just for first three tuple...i want the counter for tuple (0,2,3) or tuple (1,2,4) likewise i can pass any three position of the tuple and get the count of it... How can I do this?
If what i understood from your question is correct, the code below will solve your issue: SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] from collections import Counter def get_new_list(a, pos): # Check if any element in pos is > than the length of the tuples if any(k >= len(min(SS1, key=lambda x: len(x))) for k in pos): return for k in a: yield tuple(k[j] for j in pos) def elm_counter(elm): if not len(elm): return c = Counter(elm) for k, v in c.items(): if v > 0: print(k, v) elm = list(get_new_list(SS1, (0, 2, 4))) elm_counter(elm) print('---') elm = list(get_new_list(SS1, (1, 2, 4))) elm_counter(elm) Output: (1, 3, 5) 1 (1, 3, 6) 2 (1, 4, 6) 2 (2, 4, 6) 1 --- (2, 3, 6) 2 (2, 3, 5) 1 (3, 4, 6) 2 (2, 4, 6) 1