[Tutor] word printing issue

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jun 20 12:25:45 EDT 2019


On 20/06/2019 11:44, mhysnm1964 at gmail.com wrote:

> I have a list of strings that I want to break them into separate words, and
> a combination of words then store them into a list. Example below of a
> string:

> "Hello Python team".
> The data structure:
> [ ['Hello'],
> ['Hello', 'Python'],
> ['Hello', 'Python', 'team'],
> ]'Python'],
> ]'Python', 'team'],
> ['team'] ]
> 
>  
> 
> I want to know if there is a better method in doing this without the
> requirement of a module. 

Modules are there to be used...

Here is one with itertools from the standard library that gets close:

input = "hello Python team".split()
result = []
for n in range(len(input):
   result += [item for item in it.combinations(input,n+1)]

If you really want to do it from scratch then Google combinations
algorithm, or look on wikipedia.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list