modifying a list element from a function

T

TP

Hi everybody,

Be a the following list, containing list elements which second field is a
string.
a = [ [4, "toto"], [5, "cou"] ]
a[0][1]="tou"
a
[[4, 'tou'], [5, 'cou']]

OK.

Now, I want:
* to do the same modification on the list "a" within a function
* not to hardcode in this function the position of the string in each
element of a.

At first sight, we think at defining a function like this:

def assign( text_accessor, list_elem, new_textvalue ):

text_accessor( list_elem ) = new_textvalue

and then doing:

assign( lambda elem: elem[1], a[0], "co" )

But it does not work, we obtain:

text_accessor( list_elem ) = new_textvalue
SyntaxError: can't assign to function call

In fact, we should work on the address of text_accessor( list_elem ). How to
do that?

Thanks

Julien
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
P

Peter Otten

TP said:
Hi everybody,

Be a the following list, containing list elements which second field is a
string.
a = [ [4, "toto"], [5, "cou"] ]
a[0][1]="tou"
a
[[4, 'tou'], [5, 'cou']]

OK.

Now, I want:
* to do the same modification on the list "a" within a function
* not to hardcode in this function the position of the string in each
element of a.

At first sight, we think at defining a function like this:

def assign( text_accessor, list_elem, new_textvalue ):

text_accessor( list_elem ) = new_textvalue

and then doing:

assign( lambda elem: elem[1], a[0], "co" )

But it does not work, we obtain:

text_accessor( list_elem ) = new_textvalue
SyntaxError: can't assign to function call

In fact, we should work on the address of text_accessor( list_elem ). How
to do that?

You can understand Python to some extend if you liken its variables to
pointers, but here the analogy breaks: you cannot have pointers to
pointers. To parameterize list item access you can either pass the index or
a setter (and if needed a getter) function:
a = [ [4, "toto"], [5, "cou"] ]
def assign(setitem_n, items, value):
.... setitem_n(items, value)
........ items[1] = value
....
[[4, 'XX'], [5, 'cou']]

Peter
 
A

Adrian Dziubek

Could you explain your high level goal for this? It looks like a very
wicked way of doing things. Have You tried to read the list methods'
documentation? Maybe there you find something you need (like
list.index)?
 
A

Aaron Brady

Hi everybody,

Be a the following list, containing list elements which second field is a
string.
a = [ [4, "toto"], [5, "cou"] ]
a[0][1]="tou"
a

[[4, 'tou'], [5, 'cou']]

OK.

Now, I want:
* to do the same modification on the list "a" within a function
* not to hardcode in this function the position of the string in each
element of a.

At first sight, we think at defining a function like this:

def assign( text_accessor, list_elem, new_textvalue ):

    text_accessor( list_elem ) = new_textvalue

and then doing:

assign( lambda elem: elem[1], a[0], "co" )

But it does not work, we obtain:

    text_accessor( list_elem ) = new_textvalue
SyntaxError: can't assign to function call

In fact, we should work on the address of text_accessor( list_elem ). How to
do that?

There is always the __setitem__ method, either bound to the object in
question, or along with an instance and its class.
.... fun( ind, val )
....
a= [ None ]
f( a.__setitem__, 0, 'abc' )
a
['abc']

Or, you could require your instances to all have a method name to
accomplish it, or use operator.__setitem__.
 
T

TP

Adrian said:
Could you explain your high level goal for this? It looks like a very
wicked way of doing things. Have You tried to read the list methods'
documentation? Maybe there you find something you need (like
list.index)?

Hello,

I have a "disambiguation" function that modifies a text
contained "somewhere" in each element of a list. The way this text is
accessed by the function should depend on an external accessor, or setter,
so that any list could be processed, in a generic way.

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
G

Gabriel Genellina

I have a "disambiguation" function that modifies a text
contained "somewhere" in each element of a list. The way this text is
accessed by the function should depend on an external accessor, or
setter,
so that any list could be processed, in a generic way.

Use a "setter" function (as oposed to a "getter" that you used in your
example). A direct translation woud be:

def set_elem_1(elem, value):
elem[1] = value

def assign(setter, list_elem, new_textvalue):
setter(list_elem, new_textvalue)

assign(set_elem_1, a[0], "co")

I imagine that you process the whole list:

for elem in some_list:
set_elem_1(elem, new_value)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top