looking for code: combinations of letter in order - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: looking for code: combinations of letter in order (/thread-6317.html) |
looking for code: combinations of letter in order - Skaperen - Nov-16-2017 i am looking for code (preferably a function) that when given a sequence, such as a string of letters, will create a list or set (of strings or tuples) or tuple of all possible combinations of the elements of the given sequence, in the order present in that given sequence. the order of the resulting combinations is not important, hence a set is valid for the results. for example: given string: "skap" a valid result: ["s","sk","ska","skap","sap","sa","sp","skp","k","ka","kap","ap","a","p","kp"] other orders of the same set of strings would be valid. any sequence or set with any of these (not a complete list) would be an invalid result: "ss", "spk", "paks" given list: [ 3, 1, 4] a valid result: [[3], [3,1], [3,1,4], [3,4], [1], [1,4], [4]] again, the order does not matter, so this is also a valid result: [[4], [1,4], [3,4], [1], [3,1,4], [3], [3,1]] there should not be duplicates, so this should be considered to be an invalid result: [[4], [1,4], [1], [3,4], [1], [3,1,4], [3], [3,1]] you would not have this issue with sets: {(3,),(3,1),(3,1,4),(3,4),(1,)(1,3),(4,)} i don't know the mathematical terminology for this. i think it would be part of set theory. RE: looking for code: combinations of letter in order - heiner55 - Nov-16-2017 First try: #!/usr/bin/python3 import itertools def algo(liste): result = [ liste ] for i in range(1, len(liste)): result.extend(list(itertools.combinations(liste, i))) return result result = algo([1,2,3,4]) print(len(result), result) #done
RE: looking for code: combinations of letter in order - Skaperen - Nov-17-2017 that looks good. Danke! RE: looking for code: combinations of letter in order - heiner55 - Nov-17-2017 Bitte. https://en.wikipedia.org/wiki/Permutation https://en.wikipedia.org/wiki/Combination RE: looking for code: combinations of letter in order - heiner55 - Nov-18-2017 If you stumble across an easy algorithmn without itertools, let me know. Thanks. RE: looking for code: combinations of letter in order - Skaperen - Nov-18-2017 (Nov-18-2017, 05:04 AM)heiner55 Wrote: If you stumble across an easy algorithmn without itertools, i have not tested this idea, yet. for a given sequence of n items, iterate over range(1,2**n) as b.. get a string of bits representing b and start an empty list. iterate through this string of bits. for each bit that is 1 get the corresponding element from the given sequence at the same position and append it to the list. when the iteration of bits is done, yield the resulting list (or append it to the big list to be returned). yes, i am thinking this should be a generator or a stack of generators. RE: looking for code: combinations of letter in order - heiner55 - Nov-23-2017 Good idea. |