Skip to content

Commit 61c0743

Browse files
committed
Added permutations function to utils
1 parent 84a41ea commit 61c0743

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

domdf_python_tools/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,21 @@ def splitLen(string,n):
154154

155155
return [string[i:i+n] for i in range(0,len(string),n)]
156156

157+
158+
def permutations(data, n=2):
159+
"""
160+
Return permutations containing `n` items from `data` without any reverse duplicates
161+
162+
:type data: list or string
163+
:type n: int
164+
165+
:rtype: list of tuples
166+
"""
167+
168+
import itertools
169+
perms = []
170+
for i in itertools.permutations(data, n):
171+
"""from https://stackoverflow.com/questions/10201977/how-to-reverse-tuples-in-python"""
172+
if i[::-1] not in perms:
173+
perms.append(i)
174+
return perms

0 commit comments

Comments
 (0)