How to create all possible combinations of lines with spaces between characters? python

How to create all possible combinations of lines with spaces between characters?

[in]: "foobar" [out]: ['foobar', 'f oobar', 'fo obar', 'fo obar', 'foo bar', 'f oo bar', 'fo o bar', 'foo bar', 'foob ar', 'f oob ar', 'fo ob ar', 'fo ob ar', 'foo b ar', 'f oo b ar', 'fo ob ar', 'foob ar', 'fooba r', 'f ooba r', 'fo oba r', 'fo oba r', 'foo ba r', 'f oo ba r', 'fo o ba r', 'foo ba r', 'foob a r', 'f oob a r', 'fo ob a r', 'fo ob a r', 'foo ba r', 'f oo ba r', 'fo oba r', 'fooba r', 'foobar', 'f oobar', 'fo obar', 'fo obar', 'foo bar', 'f oo bar', 'fo o bar', 'foo bar', 'foob ar', 'f oob ar', 'fo ob ar', 'fo ob ar', 'foo b ar', 'f oo b ar', 'fo ob ar', 'foob ar', 'fooba r', 'f ooba r', 'fo oba r', 'fo oba r', 'foo ba r', 'f oo ba r', 'fo o ba r', 'foo ba r', 'foob a r', 'f oob a r', 'fo ob a r', 'fo ob a r', 'foo ba r', 'f oo ba r', 'fo oba r', 'fooba r'] 
+6
source share
7 answers
 import itertools as it def func(s): if not s: return [s] binary = it.product(['',' '], repeat=len(s)-1) zipped = (it.izip_longest(s , comb, fillvalue='') for comb in binary) return [''.join(it.chain.from_iterable(x)) for x in zipped] func('foobar') 

output:

 ['foobar', 'fooba r', 'foob ar', 'foob a r', 'foo bar', 'foo ba r', 'foo b ar', 'foo ba r', 'fo obar', 'fo oba r', 'fo ob ar', 'fo ob a r', 'fo o bar', 'fo o ba r', 'fo ob ar', 'fo oba r', 'f oobar', 'f ooba r', 'f oob ar', 'f oob a r', 'f oo bar', 'f oo ba r', 'f oo b ar', 'f oo ba r', 'fo obar', 'fo oba r', 'fo ob ar', 'fo ob a r', 'foo bar', 'foo ba r', 'foob ar', 'fooba r'] 
+3
source
 from itertools import product text = "foobar" L = [''.join(reversed(x)).rstrip() for x in product(*[(c, c+' ') for c in reversed(text)])] print L 

 ['foobar', 'f oobar', 'fo obar', 'fo obar', 'foo bar', 'f oo bar', 'fo o bar', 'foo bar', 'foob ar', 'f oob ar', 'fo ob ar', 'fo ob ar', 'foo b ar', 'f oo b ar', 'fo ob ar', 'foob ar', 'fooba r', 'f ooba r', 'fo oba r', 'fo oba r', 'foo ba r', 'f oo ba r', 'fo o ba r', 'foo ba r', 'foob a r', 'f oob a r', 'fo ob a r', 'fo ob a r', 'foo ba r', 'f oo ba r', 'fo oba r', 'fooba r', 'foobar', 'f oobar', 'fo obar', 'fo obar', 'foo bar', 'f oo bar', 'fo o bar', 'foo bar', 'foob ar', 'f oob ar', 'fo ob ar', 'fo ob ar', 'foo b ar', 'f oo b ar', 'fo ob ar', 'foob ar', 'fooba r', 'f ooba r', 'fo oba r', 'fo oba r', 'foo ba r', 'f oo ba r', 'fo o ba r', 'foo ba r', 'foob a r', 'f oob a r', 'fo ob a r', 'fo ob a r', 'foo ba r', 'f oo ba r', 'fo oba r', 'fooba r'] 
+2
source

This is probably not the most efficient way, but I would do two lists. Each has a letter like every element, and the other has each letter followed by a space. (Skip the last letter each time, because it always does not occur.) A possible interval is generated by choosing between two lists for each letter (which can be modeled as a binary number, where 0 = no space and 1 = space)

 def spacify(word): no_space = list(word[:-1]) spaced = [lt + ' ' for lt in no_space] for i in range(2 ** (len(word) - 1)): spaced_word = "" for j in range(len(word) - 1): if i % 2 == 0: spaced_word += no_space[j] else: spaced_word += spaced[j] i = i // 2 # Or use bit shifting to be fancy print spaced_word + word[-1] 
+1
source
 from itertools import combinations def gen_spaces(data): return_value = [] size = len(data)-1 for num_spaces in range(size): for comb in combinations(range(size), num_spaces+1): data_as_list = list(data) for i in comb: data_as_list[i] +=' ' return_value.append(''.join(data_as_list)) return return_value from pprint import pprint pprint(gen_spaces("foobar")) 

Output:

 ['f oobar', 'fo obar', 'foo bar', 'foob ar', 'fooba r', 'fo obar', 'f oo bar', 'f oob ar', 'f ooba r', 'fo o bar', 'fo ob ar', 'fo oba r', 'foo b ar', 'foo ba r', 'foob a r', 'foo bar', 'fo ob ar', 'fo oba r', 'f oo b ar', 'f oo ba r', 'f oob a r', 'fo ob ar', 'fo o ba r', 'fo ob a r', 'foo ba r', 'foob ar', 'foo ba r', 'fo ob a r', 'f oo ba r', 'fo oba r', 'fooba r'] 

Update:

You mentioned that you need "all possible combinations of a string with spaces between characters", but at the same time, the example you specified in [Out] does not reflect this (that is, you have "foo bar" twice, "f ooba r" absent, etc.)

In this answer, I assume that you really want "all possible combinations of string with spaces between characters"

+1
source

Here is the implementation of my recursive idea above:

 def string_spaces(s): ret = set([s]) # use a set rather than a list to prevent duplicates for i in range(1, len(s)): for fst in string_spaces(s[:i]): for snd in string_spaces(s[i:]): ret.add(fst + ' ' + snd) return ret 

Example:

 In [11]: string_spaces('foo') Out[11]: set(['foo', 'fo o', 'f oo', 'fo o']) 

NB: Python has a recursion limit of 1000 stack frames, so this will crash for very long lines (longer than 1000 characters).

+1
source

Recursive solution. (You may need to use sys.setrecursionlimit() for longer strings):

 def gen_perm(my_str): if len(my_str) <= 1 : return [my_str] rest_perms = gen_perm(my_str[1:]) all_perms = [my_str[0] + perm for perm in rest_perms ] + [my_str[0] + ' ' + perm for perm in rest_perms] return all_perms print(gen_perm("foobar")) 
+1
source

Using the itertools library (but this is almost the same as the Titandrake):

 import itertools foobar = "foobar" foobar_r = range(len(foobar)) for integer in range(2**5): binary_mask = [ bit for bit in itertools.ifilter(lambda x: ( integer >>x)&0x01, foobar_r ) ] spaces_mask = [ " " if i in binary_mask else "" for i in foobar_r ] # Zip-it Crash-it Melt-it Upgrade-it print integer, "".join([ "".join([str(char) for char in zip_char ]) for zip_char in itertools.izip(foobar,spaces_mask)]) 
0
source

Source: https://habr.com/ru/post/944698/


All Articles