Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103427 > unrolled thread

How may I change values in tuples of list of lists?

Started bysubhabangalore@gmail.com
First post2016-02-23 20:04 -0800
Last post2016-02-24 16:34 +1100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  How may I change values in tuples of list of lists? subhabangalore@gmail.com - 2016-02-23 20:04 -0800
    Re: How may I change values in tuples of list of lists? Ben Finney <ben+python@benfinney.id.au> - 2016-02-24 16:34 +1100

#103427 — How may I change values in tuples of list of lists?

Fromsubhabangalore@gmail.com
Date2016-02-23 20:04 -0800
SubjectHow may I change values in tuples of list of lists?
Message-ID<f0b9a2c6-1db4-4614-9221-585d3438c68f@googlegroups.com>
Hi 

I am trying to use the following set of tuples in list of lists. 
I am using a Python based library named, NLTK. 

>>> import nltk
>>> from nltk.corpus import brown as bn
>>> bt=bn.tagged_sents()
>>> bt_5=bt[:5]
>>> print bt
[[(u'The', u'AT'), (u'Fulton', u'NP-TL'), (u'County', u'NN-TL'), (u'Grand', u'JJ-TL'), (u'Jury', u'NN-TL'), (u'said', u'VBD'), (u'Friday', u'NR'), (u'an', u'AT'), (u'investigation', u'NN'), (u'of', u'IN'), (u"Atlanta's", u'NP$'), (u'recent', u'JJ'), (u'primary', u'NN'), (u'election', u'NN'), (u'produced', u'VBD'), (u'``', u'``'), (u'no', u'AT'), (u'evidence', u'NN'), (u"''", u"''"), (u'that', u'CS'), (u'any', u'DTI'), (u'irregularities', u'NNS'), (u'took', u'VBD'), (u'place', u'NN'), (u'.', u'.')], [(u'The', u'AT'), (u'jury', u'NN'), (u'further', u'RBR'), (u'said', u'VBD'), (u'in', u'IN'), (u'term-end', u'NN'), (u'presentments', u'NNS'), (u'that', u'CS'), (u'the', u'AT'), (u'City', u'NN-TL'), (u'Executive', u'JJ-TL'), (u'Committee', u'NN-TL'), (u',', u','), (u'which', u'WDT'), (u'had', u'HVD'), (u'over-all', u'JJ'), (u'charge', u'NN'), (u'of', u'IN'), (u'the', u'AT'), (u'election', u'NN'), (u',', u','), (u'``', u'``'), (u'deserves', u'VBZ'), (u'the', u'AT'), (u'praise', u'NN'), (u'and', u'CC'), (u'thanks', u'NNS'), (u'of', u'IN'), (u'the', u'AT'), (u'City', u'NN-TL'), (u'of', u'IN-TL'), (u'Atlanta', u'NP-TL'), (u"''", u"''"), (u'for', u'IN'), (u'the', u'AT'), (u'manner', u'NN'), (u'in', u'IN'), (u'which', u'WDT'), (u'the', u'AT'), (u'election', u'NN'), (u'was', u'BEDZ'), (u'conducted', u'VBN'), (u'.', u'.')], ...]
>>> 

Now if I want to change the values of tags like 'AT', 'NP-TL', 'NN-TL', etc. to some arbitrary ones like XX,YY,ZZ and yet preserve total structure of tuples in list of lists, please suggest how may I do it. 

I donot think it is an NLTK issue, rather a Python issue. 
I am trying to access and change but using 
for i,j in enumerate(bt_5), etc. bit long stuff.

If any one may kindly suggest a smart line of code. 

I am using Python2.7.11 on MS-Windows-10. My NLTK version is 3.1

Thanks in advance.

Regards,
RP.

[toc] | [next] | [standalone]


#103428

FromBen Finney <ben+python@benfinney.id.au>
Date2016-02-24 16:34 +1100
Message-ID<mailman.84.1456292088.20994.python-list@python.org>
In reply to#103427
subhabangalore@gmail.com writes:

> Now if I want to change the values of tags like 'AT', 'NP-TL',
> 'NN-TL', etc. to some arbitrary ones like XX,YY,ZZ and yet preserve
> total structure of tuples in list of lists, please suggest how may I
> do it. 

Changing items in lists is done by assigning to an index in that list,
using index syntax::

    big_list[17] = new_value

This works because a list is a homogeneous mutable sequence: no position
in the sequence has any special meaning, and you can change any of the
items in the sequence to refer to a different value.

Choosing the list type to represent a structure is a choice to express
“this sequence doesn't mean anything except the particular ordering of
these items, and changing any of the values doesn't change the meaning
of this sequence”.


I think your question boils down to “how do I change one item in a
tuple?”. If that's the essence of the question, the other stuff about
where the tuple is found are irrelevant to solving this.

The answer to that question is: you don't. Instead, you create a new
tuple.


A tuple is immutable; the programmer, by choosing the tuple type, has
chosen to express “this sequence is a single complex value, where the
total set of items in this order has its own meaning, and if any of its
items were different this would have a different meaning”.

So, to create a new tuple based on an existing tuple, you need to
specify each item in the new tuple. If you want some of the items in the
new tuple to be from the existing tuple, you need to access those.

    foo = ('Fulton', 'NP-TL')
    bar = tuple(foo[0], 'XX')

Is this awkward? Yes, so you should be thinking hard about whether
you're fighting against the intent of the existing data structure.

The programmer either made a bad design choice; or the choice of tuple
type was for a good purpose. You should lean toward the latter until you
know more about the program.

-- 
 \      “It seems intuitively obvious to me, which means that it might |
  `\                                           be wrong.” —Chris Torek |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web