[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

list mutability

gigs

2/18/2008 6:10:00 PM

hi im having this code

l = [1, 3, 5, 'D', 1, 2, 3, 4, 5, 6, 7, 'A', 'S', 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 'A']

why i need to copy x list? can someone explain me. If i dont copy it i get this
result:
>>> took_num_range(l)
[[1, 2, 3, 4, 5, 6, 7], [6, 5, 4, 3, 2, 1, 0], [6, 5, 4, 3, 2, 1, 0], [6, 5, 4,
3, 2, 1, 0], [6, 5, 4, 3, 2, 1, 0]]

but if i copy it i get result as im looking for
>>> took_num_range(l)
[[1, 2, 3, 4, 5, 6, 7], [9, 8, 7, 6, 5, 4, 3], [8, 7, 6, 5, 4, 3, 2], [7, 6, 5,
4, 3, 2, 1], [6, 5, 4, 3, 2, 1, 0]]
>>>

def took_num_range(l):
j = []
x = []
for i in l:
if type(i) is int and len(x) == 7:
j.append(x)
x = x[:] # im mean here
x.pop(0)
if type(i) is int and len(x) < 7:
x.append(i)
if type(i) is not int and len(x) == 7:
j.append(x)
x = []
if type(i) is not int and len(x) != 7:
x = []
return j


thx
6 Answers

John Machin

2/18/2008 7:41:00 PM

0

On Feb 19, 5:09 am, gigs <g...@hi.t-com.hr> wrote:
> hi im having this code
>
> l = [1, 3, 5, 'D', 1, 2, 3, 4, 5, 6, 7, 'A', 'S', 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 'A']
>
> why i need to copy x list? can someone explain me. If i dont copy it i get this
> result:
> >>> took_num_range(l)
> [[1, 2, 3, 4, 5, 6, 7], [6, 5, 4, 3, 2, 1, 0], [6, 5, 4, 3, 2, 1, 0], [6, 5, 4,
> 3, 2, 1, 0], [6, 5, 4, 3, 2, 1, 0]]
>
> but if i copy it i get result as im looking for
> >>> took_num_range(l)
> [[1, 2, 3, 4, 5, 6, 7], [9, 8, 7, 6, 5, 4, 3], [8, 7, 6, 5, 4, 3, 2], [7, 6, 5,
> 4, 3, 2, 1], [6, 5, 4, 3, 2, 1, 0]]
> >>>
>
> def took_num_range(l):
> j = []
> x = []
> for i in l:
> if type(i) is int and len(x) == 7:
> j.append(x)
> x = x[:] # im mean here
> x.pop(0)

j.append(x) saves a reference to the same list that x refers to. So
when you later mutate that one list, all references in j refer to the
mutated list.

Instead of
x = x[:]
x.pop(0)
which copies the contents twice, you can do
x = x[1:]

In a more complicated example, it would be better to take a copy at
each point:

j.append(x[:])

.... then you are sure you have a photo of x and it doesn't matter how/
when you mutate x later.

> if type(i) is int and len(x) < 7:

This is a big trap for the casual reader (len(x) may have changed
since the previous "if" statement) ... see below.

> x.append(i)
> if type(i) is not int and len(x) == 7:
> j.append(x)
> x = []
> if type(i) is not int and len(x) != 7:
> x = []

What do you want to do if your input ends with 7 integers and no
letter, e.g.
[1,2,3,'A',1,2,3,4,5,6,7]
?

> return j

Some suggestions:
1. use meaningful names
2. don't have hard-wired numbers like 7
3. use "else" and "elif" as appropriate to avoid repeating conditions
unnecessarily.

Here's a suggested replacement for your code, tested to the extent
shown:

C:\junk>type tooknumrange.py
def took_num_range(seq, size, grab_end=False):
result = []
queue = []
for item in seq:
if isinstance(item, int):
if len(queue) == size:
result.append(queue)
queue = queue[1:]
queue.append(item)
else:
if len(queue) == size:
result.append(queue)
queue = []
if grab_end and len(queue) == size:
result.append(queue)
return result

test1 = [
1, 3, 5, 'D',
1, 2, 3, 4, 5, 6, 7, 'A',
'S',
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 'A',
]
test1r = [
[1, 2, 3, 4, 5, 6, 7],
[9, 8, 7, 6, 5, 4, 3],
[8, 7, 6, 5, 4, 3, 2],
[7, 6, 5, 4, 3, 2, 1],
[6, 5, 4, 3, 2, 1, 0],
]
test2 = [1, 2, 3, 4, 5]
test2r = [[1, 2, 3, 4, 5]] ### or is it [] ???
tests = [
(test1, test1r, 7, False),
(test2, [], 5, False),
(test2, test2r, 5, True),
]

failed = 0
for tno, (tseq, tresult, tsize, tgrab) in enumerate(tests):
aresult = took_num_range(tseq, tsize, grab_end=tgrab)
if aresult != tresult:
failed += 1
print 'Test %d failed' % (tno+1)
print 'Expected:', tresult
print 'Actual :', aresult
print 'Failed %d test(s) out of %d' % (failed, len(tests))


C:\junk>tooknumrange.py
Failed 0 test(s) out of 3

HTH,
John

Michael Ejercito

3/30/2014 4:46:00 PM

0



"The Revd" wrote in message
news:682gj9d25htipdu85us37mlkc8bvcehl36@4ax.com...

>On Sat, 29 Mar 2014 21:25:20 -0700, "Michael Ejercito"
><mejercit@hotmail.com> wrote:

>>
>>"The Revd" <peeling@degenerate.Grik> wrote in message
>>news:9fsej9ti9f6uh416ht2jiuoqr1qma9451p@4ax.com...
>>> On Sat, 29 Mar 2014 17:43:59 -0700, "Michael Ejercito"
>>> <mejercit@hotmail.com> wrote:
>>>> Tek wished the Jewish people a happy Shabbat and this is his reply.
>>>
>>> You got a problem with that, asshole?
>> Yes.

>Greek you, then.
Fuck your denigration of the Greek people.



Michael

The Revd

3/30/2014 5:04:00 PM

0

On Sun, 30 Mar 2014 09:45:35 -0700, "Michael Ejercito"
<mejercit@hotmail.com> wrote:

>
>
>"The Revd" wrote in message
>news:682gj9d25htipdu85us37mlkc8bvcehl36@4ax.com...
>
>>On Sat, 29 Mar 2014 21:25:20 -0700, "Michael Ejercito"
>><mejercit@hotmail.com> wrote:
>
>>>
>>>"The Revd" <peeling@degenerate.Grik> wrote in message
>>>news:9fsej9ti9f6uh416ht2jiuoqr1qma9451p@4ax.com...
>>>> On Sat, 29 Mar 2014 17:43:59 -0700, "Michael Ejercito"
>>>> <mejercit@hotmail.com> wrote:
>>>>> Tek wished the Jewish people a happy Shabbat and this is his reply.
>>>>
>>>> You got a problem with that, asshole?
>>> Yes.
>
>>Greek you, then.
> Fuck your denigration of the Greek people.

Greek the Grik 'people' and greek you, gook!

Michael Ejercito

3/30/2014 7:51:00 PM

0


"The Revd" <peeling@degenerate.Grik> wrote in message
news:njjgj9pv0cias1trf3hksh7lqgn4ae3mev@4ax.com...
> On Sun, 30 Mar 2014 09:45:35 -0700, "Michael Ejercito"
> <mejercit@hotmail.com> wrote:
>
>>
>>
>>"The Revd" wrote in message
>>news:682gj9d25htipdu85us37mlkc8bvcehl36@4ax.com...
>>
>>>On Sat, 29 Mar 2014 21:25:20 -0700, "Michael Ejercito"
>>><mejercit@hotmail.com> wrote:
>>>> Yes.
>>
>>>Greek you, then.
>> Fuck your denigration of the Greek people.
>
> Greek the Grik 'people' and greek you, gook!
Greeks would be offended if they read that.


Michael


The Revd

3/31/2014 1:49:00 AM

0

On Sun, 30 Mar 2014 12:51:25 -0700, "Michael Ejercito"
<mejercit@hotmail.com> wrote:

>
>"The Revd" <peeling@degenerate.Grik> wrote in message
>news:njjgj9pv0cias1trf3hksh7lqgn4ae3mev@4ax.com...
>> On Sun, 30 Mar 2014 09:45:35 -0700, "Michael Ejercito"
>> <mejercit@hotmail.com> wrote:
>>
>>>
>>>
>>>"The Revd" wrote in message
>>>news:682gj9d25htipdu85us37mlkc8bvcehl36@4ax.com...
>>>
>>>>On Sat, 29 Mar 2014 21:25:20 -0700, "Michael Ejercito"
>>>><mejercit@hotmail.com> wrote:
>>>>> Yes.
>>>
>>>>Greek you, then.
>>> Fuck your denigration of the Greek people.
>>
>> Greek the Grik 'people' and greek you, gook!
> Greeks would be offended if they read that.

Would they? Greek them, then!

The Revd

4/1/2014 10:31:00 PM

0

On Sun, 30 Mar 2014 12:51:25 -0700, "Michael Ejercito"
<mejercit@hotmail.com> wrote:

>
>"The Revd" <peeling@degenerate.Grik> wrote in message
>news:njjgj9pv0cias1trf3hksh7lqgn4ae3mev@4ax.com...
>> On Sun, 30 Mar 2014 09:45:35 -0700, "Michael Ejercito"
>> <mejercit@hotmail.com> wrote:
>>
>>>
>>>
>>>"The Revd" wrote in message
>>>news:682gj9d25htipdu85us37mlkc8bvcehl36@4ax.com...
>>>
>>>>On Sat, 29 Mar 2014 21:25:20 -0700, "Michael Ejercito"
>>>><mejercit@hotmail.com> wrote:
>>>>> Yes.
>>>
>>>>Greek you, then.
>>> Fuck your denigration of the Greek people.
>>
>> Greek the Grik 'people' and greek you, gook!
> Greeks would be offended if they read that.

Would they? Greek them, then!