Re: [Tutor] could someone explain why this happens to me.

2009-03-08 Thread Lie Ryan

Sander Sweers wrote:

2009/3/7 Alan Gauld :

mycopy = original[:]

Returns a slice of the original list. In this case it so happens
the slice is the full list.


mycopy = list(original)

Use the list type constructor to make a list out of its argument.
It just so happens the argument in this case is a list.


Both not give the desired result with nested lists and this is why you have..


What is the desired result?

Sometimes I might want to copy a list, but keep the referenced items the 
same. In which case I'd use the slice copying or list constructor copying.


Any way, there is one unmentioned way to copy a list:

listcopy = [x for x in mylist]

though it might be slower and more verbose.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] could someone explain why this happens to me.

2009-03-07 Thread Sander Sweers
2009/3/7 Alan Gauld :
>> mycopy = original[:]
>
> Returns a slice of the original list. In this case it so happens
> the slice is the full list.
>
>> mycopy = list(original)
>
> Use the list type constructor to make a list out of its argument.
> It just so happens the argument in this case is a list.

Both not give the desired result with nested lists and this is why you have..

>> mycopy = copy.deepcopy(original)
>
> calls the deepcopy function which traverses the original list
> and all nested structures explicitly copying the elements.

copy.deepcopy :-)

>>> import copy
>>> list1 = [1,2]
>>> list2 = [3.4]
>>> list3 = [list1, list2]
>>> list4 = list(list3)
>>> list5 = list3[:]
>>> list6 = copy.deepcopy(list3)
>>> list1.append('a')
>>> list1
[1, 2, 'a']
>>> list3
[[1, 2, 'a'], [3.3999]]
>>> list4
[[1, 2, 'a'], [3.3999]]
>>> list5
[[1, 2, 'a'], [3.3999]]
>>> list6
[[1, 2], [3.3999]]

Notice that list6 is the only list which does not have the appended a.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] could someone explain why this happens to me.

2009-03-07 Thread عماد نوفل
2009/3/7 Alan Gauld 

>
> "Emad Nawfal (عماد نوفل)"  wrote
>
>  As a linguist, I would love to know what the difference between these
>> things are:
>>
>
> The differences are the same whether you are a linguist or not :-)
> (and yes I know my reading is incorrect grammaticallly but I couldn't
> resist it!)
>
>  mycopy = original[:]
>>
> Returns a slice of the original list. In this case it so happens
> the slice is the full list.
>
>  mycopy = copy.deepcopy(original)
>>
>
> calls the deepcopy function which traverses the original list
> and all nested structures explicitly copying the elements.
>
>  mycopy = list(original)
>>
>
> Use the list type constructor to make a list out of its argument.
> It just so happens the argument in this case is a list.
>
> The end result is the same but the mechanisms (ie the code called)
> are quite different. It would be interesting to compare speeds,
> but I'm too lazy! I suspect deepcopy would be slowest but I'm
> not sure about the other two.
>
> Alan G.
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


Thanks Alan,
As a linguist, I appreciate your disambiiguation mechanism.
Not all subscribers to this list are programmers, or know much about
computer science. I know lots about formal thinking, but not about
programming, and I always preface my questions with an expression that
means: please give me a simple answer without so much jargon.
Thanks for the explanation
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] could someone explain why this happens to me.

2009-03-07 Thread Alan Gauld


"Emad Nawfal (عماد نوفل)"  wrote

As a linguist, I would love to know what the difference between 
these

things are:


The differences are the same whether you are a linguist or not :-)
(and yes I know my reading is incorrect grammaticallly but I couldn't 
resist it!)



mycopy = original[:]

Returns a slice of the original list. In this case it so happens
the slice is the full list.


mycopy = copy.deepcopy(original)


calls the deepcopy function which traverses the original list
and all nested structures explicitly copying the elements.


mycopy = list(original)


Use the list type constructor to make a list out of its argument.
It just so happens the argument in this case is a list.

The end result is the same but the mechanisms (ie the code called)
are quite different. It would be interesting to compare speeds,
but I'm too lazy! I suspect deepcopy would be slowest but I'm
not sure about the other two.

Alan G.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] could someone explain why this happens to me.

2009-03-07 Thread عماد نوفل
On Sat, Mar 7, 2009 at 2:36 PM, Kapsicum  wrote:

>
>
> On Sun, Mar 8, 2009 at 12:39 AM, sphennings W. wrote:
>
>> When I enter the following code into IDLE  do both lists have the same
>> value?
>> How would I manipulate both lists separately?
>>
>> >>> List1=[1,2,3]
>> >>> List2=List1
>> >>> List2.reverse()
>> >>> print(List2)
>> [3, 2, 1]
>> >>> print(List1)
>> [3, 2, 1]
>> >>> List2.append(0)
>> >>> print(List2)
>> [3, 2, 1, 0]
>> >>> print(List1)
>> [3, 2, 1, 0]
>
>
> When you create an object and assign it to a variable, the variable only
> refers to the object
> and does not represent the object itself.
>
>
> If you want to make a copy of a list or such kinds of sequences , then you
> have to use
> the slicing operation to make a copy. If you just assign the variable name
> to another name,
> both of them will refer to the same object.
>
> List2=List1[ : ]
>
>
>
> Kapil Dua
> Mobile: +919711311052
>
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
> This made me search, and I found another solution: if you use the list
function as follows:
>>> s = [1,2,3]
>>> v = list(s)
>>> s.reverse()
>>> s
[3, 2, 1]
>>> v
[1, 2, 3]
>>>

As a linguist, I would love to know what the difference between  these
things are:
mycopy = original[:]
mycopy = copy.deepcopy(original)
mycopy = list(original)


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] could someone explain why this happens to me.

2009-03-07 Thread Kapsicum
On Sun, Mar 8, 2009 at 12:39 AM, sphennings W.  wrote:

> When I enter the following code into IDLE  do both lists have the same
> value?
> How would I manipulate both lists separately?
>
> >>> List1=[1,2,3]
> >>> List2=List1
> >>> List2.reverse()
> >>> print(List2)
> [3, 2, 1]
> >>> print(List1)
> [3, 2, 1]
> >>> List2.append(0)
> >>> print(List2)
> [3, 2, 1, 0]
> >>> print(List1)
> [3, 2, 1, 0]


When you create an object and assign it to a variable, the variable only
refers to the object
and does not represent the object itself.


If you want to make a copy of a list or such kinds of sequences , then you
have to use
the slicing operation to make a copy. If you just assign the variable name
to another name,
both of them will refer to the same object.

List2=List1[ : ]



Kapil Dua
Mobile: +919711311052
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] could someone explain why this happens to me.

2009-03-07 Thread Emile van Sebille

sphennings W. wrote:

When I enter the following code into IDLE  do both lists have the same value?


They way you've done it, both names List1 and List2 refer/point to the 
same list.  Changes to one affect both.



How would I manipulate both lists separately?


Assign them separately or use a copy form of assignment, eg

List2 = List1[:]

Note however that this form of copy doesn't copy nested structures and 
you'll have similar issues in that case.  Look into deepcopy.


Emile




List1=[1,2,3]
List2=List1
List2.reverse()
print(List2)

[3, 2, 1]

print(List1)

[3, 2, 1]

List2.append(0)
print(List2)

[3, 2, 1, 0]

print(List1)

[3, 2, 1, 0]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] could someone explain why this happens to me.

2009-03-07 Thread عماد نوفل
On Sat, Mar 7, 2009 at 2:09 PM, sphennings W.  wrote:

> When I enter the following code into IDLE  do both lists have the same
> value?
> How would I manipulate both lists separately?
>
> >>> List1=[1,2,3]
> >>> List2=List1
> >>> List2.reverse()
> >>> print(List2)
> [3, 2, 1]
> >>> print(List1)
> [3, 2, 1]
> >>> List2.append(0)
> >>> print(List2)
> [3, 2, 1, 0]
> >>> print(List1)
> [3, 2, 1, 0]
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

When you assign list2 to list1, you are just referencing the old list.
Whatever you do either one, it will affect the other as well. The solution,
to my limited knowledge, is to deep-copy the list, as illustrated here:
>>> l1 = [1,2,3]
>>> import copy
>>> l2 = copy.deepcopy(l1)
>>> l2
[1, 2, 3]
>>> l1.reverse()
>>> l1
[3, 2, 1]
>>> l2
[1, 2, 3]
>>>


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] could someone explain why this happens to me.

2009-03-07 Thread sphennings W.
When I enter the following code into IDLE  do both lists have the same value?
How would I manipulate both lists separately?

>>> List1=[1,2,3]
>>> List2=List1
>>> List2.reverse()
>>> print(List2)
[3, 2, 1]
>>> print(List1)
[3, 2, 1]
>>> List2.append(0)
>>> print(List2)
[3, 2, 1, 0]
>>> print(List1)
[3, 2, 1, 0]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor