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


Groups > comp.lang.python > #103428

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

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: How may I change values in tuples of list of lists?
Date Wed, 24 Feb 2016 16:34:35 +1100
Lines 55
Message-ID <mailman.84.1456292088.20994.python-list@python.org> (permalink)
References <f0b9a2c6-1db4-4614-9221-585d3438c68f@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de Ib7TBGStP16vjsyYX+DQlAfiy8Y/7R+BE4+f1otCJvbA==
Cancel-Lock sha1:pawb3k4y32mxonLLBZlrAmmcHTc=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'purpose.': 0.07; 'type,': 0.07; 'subject:How': 0.09; 'assigning': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'index': 0.13; 'suggest': 0.15; 'value.': 0.15; "'xx')": 0.16; 'immutable;': 0.16; 'programmer,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'sequence:': 0.16; 'structure.': 0.16; 'subject:may': 0.16; 'subject:values': 0.16; 'tuple,': 0.16; 'programmer': 0.18; 'latter': 0.22; 'tuples': 0.22; 'seems': 0.23; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'subject:list': 0.26; 'header:X -Complaints-To:1': 0.26; 'question': 0.27; 'sequence': 0.27; 'specify': 0.27; 'this.': 0.28; 'values': 0.28; 'fighting': 0.29; 'preserve': 0.29; 'subject:lists': 0.32; 'choosing': 0.33; 'foo': 0.33; 'instead,': 0.33; 'subject:change': 0.33; 'changing': 0.34; 'structure': 0.34; 'except': 0.34; 'lists': 0.34; 'list': 0.34; 'so,': 0.35; 'ones': 0.35; 'done': 0.35; 'question,': 0.35; 'solving': 0.35; 'express': 0.35; 'item': 0.35; 'list,': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'stuff': 0.38; 'anything': 0.38; 'mean': 0.38; 'represent': 0.38; 'means': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.62; 'total': 0.62; 'more': 0.63; 'different': 0.63; 'intent': 0.66; 'special': 0.73; 'obvious': 0.76; '_o__)': 0.84; 'irrelevant': 0.84; 'lean': 0.84; 'received:125': 0.84; '\xe2\x80\x9cthis': 0.84; 'essence': 0.91; 'items,': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103428

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web