Re: [Tutor] Unintentionally manipulating a list

2011-02-25 Thread Steven D'Aprano

ranjan das wrote:

I am facing the following problem


I have a list of the form

INPUT= [ [ ['A1-N','A2-Y','A3-N' ],['B1-Y','B2-N','B3-N' ] ], [.] ]



and I want an output of the form (selecting only those elements which have a
Y associated with them)

OUTPUT=[ ['A2-Y', 'B1-Y'],[] ]


Can the Y be anywhere, or must it be at the end of the string? Should 
'Y2-N' be included or excluded? I will assume excluded.


What about lowercase 'y'? Should 'A3-y' be included? I assume it should be.




I wrote the following code. Although it gives me the desired output, it
CHANGES the list INPUT


Start with a simple function that extracts the strings you want from the 
inner-most list, Here's the hard way:


def select(items, suffix):
suffix = suffix.upper()
result = []
for s in items:
if s.upper().endswith(suffix):
result.append(s)
return result


This can be simplified to:

def select(items, suffix):
suffix = suffix.upper()
return [s for s in items if s.upper().endswith(suffix)]


Now you can use it:



OUTPUT = []
# INPUT looks like:
# [ [a1_list, b1_list, ...], [a2_list, b2_list, ...], ... ]
for middle in INPUT:
# middle looks like [a99_list, b99_list, c99_list, ...]
temp = []
for inner in middle:
# inner looks like [astr-N, bstr-Y, cstr-Y, ...]
temp.extend(select(inner, '-Y'))
OUTPUT.append(temp)


There is no need to make a copy of the input list.



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unintentionally manipulating a list

2011-02-25 Thread Knacktus

Am 25.02.2011 15:49, schrieb ranjan das:


I am facing the following problem


I have a list of the form

INPUT= [ [ ['A1-N','A2-Y','A3-N' ],['B1-Y','B2-N','B3-N' ] ], [.] ]


and I want an output of the form (selecting only those elements which
have a Y associated with them)

OUTPUT=[ ['A2-Y', 'B1-Y'],[] ]


I wrote the following code. Although it gives me the desired output, it
CHANGES the list INPUT

now after i run the code I get INPUT as the same as OUTPUT (which i dont
want to happen). I have used the copy function but it still is not
working. Any help or pointers is appreciated

_CODE_

from copy import copy

temp=copy( INPUT )
OUTPUT=temp



for i in range(len(temp)):

 for j in range(len(temp[i])):

 for k in range(len(temp[i][j])):

 if temp[i][j][k][-1]=='Y':

 OUTPUT[i][j]=temp[i][j][k]


There's no need for an index. You can iterate over the elements directly 
and create the result on the fly:


results = []
for sub_list in INPUT:
sub_results = []
for sub_sub_list in sub_list:
sub_sub_results = []
for entry in sub_sub_list:
if entry.endswith("Y"):
sub_sub_results.append(entry)
if sub_sub_results:
sub_results.append(sub_sub_results)
if sub_results:
results.append(sub_results)

Uuppsy daisy, not really readable code ;-). Your solution looks cleaner. 
I bet, some of the gurus can come up with a real pythonic solution.


Cheers,

Jan








___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unintentionally manipulating a list

2011-02-25 Thread Alex Hall
You may want to deepcopy instead of just copy?

On 2/25/11, ranjan das  wrote:
> I am facing the following problem
>
>
> I have a list of the form
>
> INPUT= [ [ ['A1-N','A2-Y','A3-N' ],['B1-Y','B2-N','B3-N' ] ], [.] ]
>
>
> and I want an output of the form (selecting only those elements which have a
> Y associated with them)
>
> OUTPUT=[ ['A2-Y', 'B1-Y'],[] ]
>
>
> I wrote the following code. Although it gives me the desired output, it
> CHANGES the list INPUT
>
> now after i run the code I get INPUT as the same as OUTPUT (which i dont
> want to happen). I have used the copy function but it still is not working.
> Any help or pointers is appreciated
>
> *CODE*
>
> from copy import copy
>
> temp=copy( INPUT )
> OUTPUT=temp
>
>
>
> for i in range(len(temp)):
>
> for j in range(len(temp[i])):
>
> for k in range(len(temp[i][j])):
>
> if temp[i][j][k][-1]=='Y':
>
> OUTPUT[i][j]=temp[i][j][k]
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Unintentionally manipulating a list

2011-02-25 Thread ranjan das
I am facing the following problem


I have a list of the form

INPUT= [ [ ['A1-N','A2-Y','A3-N' ],['B1-Y','B2-N','B3-N' ] ], [.] ]


and I want an output of the form (selecting only those elements which have a
Y associated with them)

OUTPUT=[ ['A2-Y', 'B1-Y'],[] ]


I wrote the following code. Although it gives me the desired output, it
CHANGES the list INPUT

now after i run the code I get INPUT as the same as OUTPUT (which i dont
want to happen). I have used the copy function but it still is not working.
Any help or pointers is appreciated

*CODE*

from copy import copy

temp=copy( INPUT )
OUTPUT=temp



for i in range(len(temp)):

for j in range(len(temp[i])):

for k in range(len(temp[i][j])):

if temp[i][j][k][-1]=='Y':

OUTPUT[i][j]=temp[i][j][k]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor