python - How do I create a list index that loops through integers in another list -
i have list:
sentences = ['the number of adults read @ least 1 novel within past 12 months fell 47%.', 'fiction reading rose 2002 2008.', 'the decline in fiction reading last year occurred among men.', 'women read more fiction.', '50% more.', 'though decreased 10% on last decade.', 'men more read nonfiction.', 'young adults more read fiction.', 'just 54% of americans cracked open book of kind last year.', 'but novels have suffered more nonfiction.']
and have list containing indexes of sequences of sentences in above list contain number.
index_groupings = [[0, 1], [4, 5], [8]]
i want extract specified sentence sequences in variable "sentences" using indexes in variable "index_groupings" following output:
the number of adults read @ least 1 novel within past 12 months fell 47%.fiction reading rose 2002 2008.
50% more.though decreased 10% on last decade.
just 54% of americans cracked open book of kind last year.
so following:
for grouping in index_groupings: if len(grouping) > 1: print sentences[grouping[:]] else: print sentences[grouping[0]]
when run that, error message says
typeerror: list indices must integers, not list
the line print sentences[grouping[:]]
trips up. there way loop through index sequences in list index_groupings returns correct output?
print ["".join(map(lambda x:sentences[x],i)) in index_groupings]
you can use join
, list comprehension
. here.
output:['the number of adults read @ least 1 novel within past 12 months fell 47%. fiction reading rose 2002 2008.', '50% more. though decreased 10% on last decade.', 'just 54% of americans cracked open book of kind last year.']
Comments
Post a Comment