List problem

Discussion in 'Python' started by subhabangalore@gmail.com, Dec 2, 2012.

  1. Guest

    Dear Group,

    I have a list of the following pattern,

    [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), ('Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag', 'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN'), ('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'NN'), ('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), ('in', 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','), ("''", "''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'), ('Capt', 'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said','VBD'), ('here', 'RB')]

    Now, as we see it has multiple VBD elements.
    I want to recognize,count and index them all.

    If any one can kindly suggest.

    Regards,
    Subhabrata
     
    , Dec 2, 2012
    #1
    1. Advertising

  2. Thomas Bach Guest

    On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote:
    >
    > len([x for x in l if x[1] == 'VBD'])
    >


    Another way is

    sum(1 for x in l if x[1] == 'VBD')

    which saves the list creation.

    Regards,
    Thomas.
     
    Thomas Bach, Dec 2, 2012
    #2
    1. Advertising

  3. Guest

    On Sunday, December 2, 2012 9:29:22 PM UTC+5:30, Thomas Bach wrote:
    > On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote:
    >
    > >

    >
    > > len([x for x in l if x[1] == 'VBD'])

    >
    > >

    >
    >
    >
    > Another way is
    >
    >
    >
    > sum(1 for x in l if x[1] == 'VBD')
    >
    >
    >
    > which saves the list creation.
    >
    >
    >
    > Regards,
    >
    > Thomas.


    Thanks. After I posted I got a solution as,
    [x for x, y in enumerate(chunk_word) if "/VB" in y]
    but you are smarter.
    Thanks.
    Regards,
    Subhabrata.
     
    , Dec 2, 2012
    #3
  4. Guest

    On Sunday, December 2, 2012 9:29:22 PM UTC+5:30, Thomas Bach wrote:
    > On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote:
    >
    > >

    >
    > > len([x for x in l if x[1] == 'VBD'])

    >
    > >

    >
    >
    >
    > Another way is
    >
    >
    >
    > sum(1 for x in l if x[1] == 'VBD')
    >
    >
    >
    > which saves the list creation.
    >
    >
    >
    > Regards,
    >
    > Thomas.


    Thanks. After I posted I got a solution as,
    [x for x, y in enumerate(chunk_word) if "/VB" in y]
    but you are smarter.
    Thanks.
    Regards,
    Subhabrata.
     
    , Dec 2, 2012
    #4
  5. Neil Cerutti Guest

    On 2012-12-02, Thomas Bach <-mainz.de> wrote:
    > On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote:
    >>
    >> len([x for x in l if x[1] == 'VBD'])
    >>

    >
    > Another way is
    >
    > sum(1 for x in l if x[1] == 'VBD')
    >
    > which saves the list creation.


    To also index them:

    vbdix = [i for i, a in emumerate(l) if a[1] == 'VBD']
    vbdno = len(indices)

    --
    Neil Cerutti
     
    Neil Cerutti, Dec 3, 2012
    #5
  6. John Gordon Guest

    In <> writes:

    > Dear Group,


    > I have a list of the following pattern,


    > [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), (=
    > 'Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag'=
    > , 'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN=
    > '), ('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'N=
    > N'), ('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), =
    > ('in', 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','=
    > ), ("''", "''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'=
    > ), ('Capt', 'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said',=
    > 'VBD'), ('here', 'RB')]


    > Now, as we see it has multiple VBD elements.
    > I want to recognize,count and index them all.


    That depends on exactly what you mean by 'reorganize' and 'index'. But
    here's a start:

    items = [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'),
    ('Commander', 'NNP'), ('Lt', 'NNP'), ('Gen', 'NNP'),
    ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag' , 'NNP'),
    ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'),
    ('chief', 'NN'), ('on', 'IN'), ('the', 'DT'),
    ('operational', 'JJ'), ('preparedness', 'NN'), ('and', 'CC'),
    ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'),
    ('in', 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'),
    (',', ','), ("''", "''"), ('defence', 'NN'),
    ('spokesperson', 'NN'), ('Group', 'NNP'), ('Capt', 'NNP'),
    ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said', 'VBD'),
    ('here', 'RB')]

    vbds = [item[0] for item in items if item[1] == 'VBD']

    print vbds
     
    John Gordon, Dec 3, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    792
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    505
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    725
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    428
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,328
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page