List with tuples in python -
List with tuples in python -
i'm new python , have questions lists , tuples. i've got list consisting of tuples sentences , wordclass-tags. 1 element in list:
[('it', 'pps'), ('says', 'vbz'), ('that', 'cs'), ('``', '``'), ('in', 'in'), ('the', 'at'), ('event', 'nn'), ('congress', 'np'), ('does', 'doz'), ('provide', 'vb'), ('this', 'dt'), ('increase', 'nn'), ('in', 'in'), ('federal', 'jj'), ('funds', 'nns'), ("''", "''"), (',', ','), ('the', 'at'), ('state', 'nn-tl'), ('board', 'nn-tl'), ('of', 'in-tl'), ('education', 'nn-tl'), ('should', 'md'), ('be', 'be'), ('directed', 'vbn'), ('to', 'to'), ('``', '``'), ('give', 'vb'), ('priority', 'nn'), ("''", "''"), ('to', 'in'), ('teacher', 'nn'), ('pay', 'nn'), ('raises', 'nns'), ('.', '.')]
as can see each word has wordclass-tag. how can search after word + wordclass in list? f.ex. if see if element contains word "federal" attached wordclass-tag "jj" ?
help much appreciated
to check if have word 'federal' tagged 'jj' on list:
your_list = [('it', 'pps'), ('says', 'vbz'), ('that', 'cs'), ('``', '``'), ('in', 'in'), ('the', 'at'), ('event', 'nn'), ('congress', 'np'), ('does', 'doz'), ('provide', 'vb'), ('this', 'dt'), ('increase', 'nn'), ('in', 'in'), ('federal', 'jj'), ('funds', 'nns'), ("''", "''"), (',', ','), ('the', 'at'), ('state', 'nn-tl'), ('board', 'nn-tl'), ('of', 'in-tl'), ('education', 'nn-tl'), ('should', 'md'), ('be', 'be'), ('directed', 'vbn'), ('to', 'to'), ('``', '``'), ('give', 'vb'), ('priority', 'nn'), ("''", "''"), ('to', 'in'), ('teacher', 'nn'), ('pay', 'nn'), ('raises', 'nns'), ('.', '.')] print ('federal', 'jj') in your_list
using list comprehension syntax can more interesting things list, illustration see tags of occurrences of word:
print " ".join([wordclass word, wordclass in your_list if word == 'federal'])
it's build functions doing generic operations on info construction work with, checking if contains word or tag:
def hasword(l, word): w, wordclass in l: if w == word: homecoming true homecoming false def hastag(l, tag): w, wordclass in l: if wordclass == tag: homecoming true homecoming false if hastag(your_list, 'jj'): print your_list
to reply question in comments:
for sentence in sentences: if ('federal', 'jj') in sentence: print sentence
python list tuples
Comments
Post a Comment