Reputation: 1353
Somewhat a python/programming newbie here.
I am trying to access a specified range of tuples from a list of tuples, but I only want to access the first element from the range of tuples. The specified range is based on a pattern I am looking for in a string of text that has been tokenized and tagged by nltk. My code:
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
text = "It is pretty good as far as driveway size is concerned, otherwise I would skip it"
tokenized = word_tokenize(text)
tagged = pos_tag(tokenized)
def find_phrase():
counter = -1
for tag in tagged:
counter += 1
if tag[0] == "as" and tagged[counter+6][0] == "concerned":
print tagged[counter:counter+7]
find_phrase()
Printed output:
[('as', 'IN'), ('far', 'RB'), ('as', 'IN'), ('driveway', 'NN'), ('size', 'NN'), ('is', 'VBZ'), ('concerned', 'VBN')]
What I actually want:
['as', 'far', 'as', 'driveway', 'size', 'is', 'concerned']
Is it possible to modify the my line of code print tagged[counter:counter+7]
to get my desired printed output?
Upvotes: 0
Views: 766
Reputation: 7688
Probably the simplest method uses a list comprehension. This statement creates a list from the first element of every tuple in your list:
print [tup[0] for tup in tagged[counter:counter+7]]
Or just for fun, if the tuples are always pairs, you could flatten the list (using any method you like) and then print every second element with the step notation of python's slice notation:
print list(sum(tagged[counter:counter+7], ()))[::2]
Or use map
with the itemgetter
function, which calls the __getitem__()
method to retrieve the 0th index of every tuple in your list:
from operator import itemgetter
print map(itemgetter(0), tagged[counter:counter+7])
Anything else? I'm sure there are more.
Upvotes: 3
Reputation: 16940
You can use like this:
result, _ = zip(*find_phrase())
print result
Upvotes: 2