0

So this is my first list:

listone = [
           'Every day Miss Leeson went out to work', 
           ' At night she brought home papers with handwriting on them and made copies with her typewriter', 
           ' Sometimes she had no work at night, and then she would sit on the steps of the high stoop with the other roomers', 
           ' Miss Leeson was not intended for a sky-light room when the plans were drawn for her creation', 
           ' She was gay-hearted and full of tender, whimsical fancies', 
           ' Once she let Mr', 
           ' Skidder read to her three acts of his great (unpublished) comedy, "It'
          ]

In this list every sentence is an element, i would like to make every sentence a sublist, or only parts of the sentence a sublist, for example:

listtwo = [['Every','Miss','work'], ['At', 'she', 'with'] etc.]

The words in the sublists were chosen are random, i already have a filtering function to use for that.

Thanks you for the help!

2
  • it will be in group of three in each sublist?? always from original list Commented Nov 7, 2014 at 16:13
  • [s.split() for s in listone] Commented Nov 7, 2014 at 16:20

3 Answers 3

4

You can also use the map function:

>>> map(str.split, listone)
[['Every', 'day', 'Miss', 'Leeson', 'went', 'out', 'to', 'work'], ['At', 'night', 'she', 'brought', 'home', 'papers', 'with', 'handwriting', 'on', 'them', 'and', 'made', 'copies', 'with', 'her', 'typewriter'], ['Sometimes', 'she', 'had', 'no', 'work', 'at', 'night,', 'and', 'then', 'she', 'would', 'sit', 'on', 'the', 'steps', 'of', 'the', 'high', 'stoop', 'with', 'the', 'other', 'roomers'], ['Miss', 'Leeson', 'was', 'not', 'intended', 'for', 'a', 'sky-light', 'room', 'when', 'the', 'plans', 'were', 'drawn', 'for', 'her', 'creation'], ['She', 'was', 'gay-hearted', 'and', 'full', 'of', 'tender,', 'whimsical', 'fancies'], ['Once', 'she', 'let', 'Mr'], ['Skidder', 'read', 'to', 'her', 'three', 'acts', 'of', 'his', 'great', '(unpublished)', 'comedy,', '"It']]

And chain it with your filter function (in this case, select 3 random words)

>>> import random
>>> map(lambda x: random.sample(x, 3), map(str.split, listone))
[['Every','Miss','work'], ['At', 'she', 'with'] etc.]

(or, with list comprehension:)

>>> [random.sample(x.split(), 3) for x in listone]
Sign up to request clarification or add additional context in comments.

Comments

3

You can use a list comprehension with split to break up the sentences

>>> [i.split() for i in listone]
[['Every', 'day', 'Miss', 'Leeson', 'went', 'out', 'to', 'work'],
 ['At', 'night', 'she', 'brought', 'home', 'papers', 'with', 'handwriting', 'on', 'them', 'and', 'made', 'copies', 'with', 'her', 'typewriter'],
 ['Sometimes', 'she', 'had', 'no', 'work', 'at', 'night,', 'and', 'then', 'she', 'would', 'sit', 'on', 'the', 'steps', 'of', 'the', 'high', 'stoop', 'with', 'the', 'other', 'roomers'],
 ['Miss', 'Leeson', 'was', 'not', 'intended', 'for', 'a', 'sky-light', 'room', 'when', 'the', 'plans', 'were', 'drawn', 'for', 'her', 'creation'],
 ['She', 'was', 'gay-hearted', 'and', 'full', 'of', 'tender,', 'whimsical', 'fancies'],
 ['Once', 'she', 'let', 'Mr'],
 ['Skidder', 'read', 'to', 'her', 'three', 'acts', 'of', 'his', 'great', '(unpublished)', 'comedy,', '"It']]

You can also use filter in the list comprehension to apply your filter function to remove the unwanted words.

5 Comments

OP does't want like that i think
What makes you think that?
I think from every sentence OP want three random word in sublist
No, they randomly picked words in their example. They said they have a filter function to select the words. So they can use map or filter for that.
Thanks for the great and fast respons, i feel dumb now, but happy.
0

if you want 3 random from each sentence :

 def get_random(a):
     return random.sample(a,3)
 [ get_random(x.split()) for x in listone ]
 '''or using map function'''
 map(get_random,map(str.split,listone))

output:

[['to', 'went', 'out'], ['on', 'At', 'with'], ['she', 'and', 'would'], ['her', 'room', 'were'], ['tender,', 'was', 'full'], ['Mr', 'let', 'Once'], ['Skidder', 'her', '(unpublished)']]

1 Comment

its random.randint, it can choose same integer again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.