Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 31st, 2012, 12:57 PM
norm850 norm850 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 15 norm850 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 13 m 33 sec
Reputation Power: 0
Appending two lists together?

Hey guys,

We use sage to code with python, and I am not sure if it's sage or if it's just python, but what I want to do is:

list = [[0, 0], [0, 0], [0, 1], [0, 1]]
list[0].append(0)

and I want for the list to now be:

[[0,0,0], [0,0], [0,1], [0,1]]

But it's doing this:
[[0,0,0], [0,0,0], [0,1], [0,1]]

Any help with this??

Reply With Quote
  #2  
Old October 31st, 2012, 01:46 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
On Python 2.6.5 (but other versions are not likely to differ):
Code:
>>> list = [[0,0], [0,0], [0,1], [0,1]]
>>> list[0].append(0)
>>> list
[[0, 0, 0], [0, 0], [0, 1], [0, 1]]

Please post an actual code example that shows the behavior in your post.

Reply With Quote
  #3  
Old October 31st, 2012, 03:12 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,345 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 6 h 47 m 59 sec
Reputation Power: 383
Doesn't look like the problem involves Macsyma, but it might.

You probably created your original list equivalently thus:
Code:
>>> ZZ=[0,0]
>>> ZO=[0,1]
>>> LIST = [ZZ,ZZ,ZO,ZO]
>>> LIST
[[0, 0], [0, 0], [0, 1], [0, 1]]
>>> LIST[0].append('references the same object')
>>> LIST
[[0, 0, 'references the same object'], [0, 0, 'references the same object'], [0, 1], [0, 1]]
>>> 


The recent posts in this thread contain expanations.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old October 31st, 2012, 03:28 PM
norm850 norm850 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 15 norm850 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 13 m 33 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Doesn't look like the problem involves Macsyma, but it might.

You probably created your original list equivalently thus:
Code:
>>> ZZ=[0,0]
>>> ZO=[0,1]
>>> LIST = [ZZ,ZZ,ZO,ZO]
>>> LIST
[[0, 0], [0, 0], [0, 1], [0, 1]]
>>> LIST[0].append('references the same object')
>>> LIST
[[0, 0, 'references the same object'], [0, 0, 'references the same object'], [0, 1], [0, 1]]
>>> 


The recent posts in this thread contain expanations.


I will post my code after class, be on the lookout within the next hour or so, thanks for the help!

Reply With Quote
  #5  
Old November 4th, 2012, 08:15 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 480 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 57 sec
Reputation Power: 63
When you work with mutable objects like lists you have to be careful about doing an alias copy:
Code:
# testing aliased (same address) objects
q = [0, 0]

mylist = [q, q]

# mylist[0] and mylist[1] have the same address
print(id(mylist[0]), id(mylist[1]))

'''possible result (same id address) >>>
(40117872, 40117872)
'''

# q[:] makes a true copy of list q and avoids alias copy
# [:] is a slicing operator
mylist = [q, q[:]]

# test it ...
print(id(mylist[0]), id(mylist[1]))

'''possible result (different id address) >>>
(40117872, 40119112)
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : November 4th, 2012 at 09:04 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Appending two lists together?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap