[Tutor] word printing issue

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Thu Jun 20 20:00:10 EDT 2019


Thanks, so much to learn.

-----Original Message-----
From: Tutor <tutor-bounces+mhysnm1964=gmail.com at python.org> On Behalf Of
Alan Gauld via Tutor
Sent: Friday, 21 June 2019 2:26 AM
To: tutor at python.org
Subject: Re: [Tutor] word printing issue

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


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list