Hi, I'm new to Python

This is the place for queries that don't fit in any of the other categories.

Hi, I'm new to Python

Postby morsebl » Fri Jul 26, 2013 3:25 pm

Actually I had about 2 weeks of Python about 8 years ago for a class but I'm not trying to learn it to work with ESRI products. I've got a book and I was working through the sample problems and I hit a snag. I know this might look wrong to y'all but the problem said that all of these things must be run on the original list. So I tried to just keep the original list and reuse it. After seeing the solution from the author that might have been the wrong way but I want to know what is going on here anyway for future problems.

here is my code:

Code: Select all
mylist = ["Athens", "Barcelona", "Cairo", "Florence", "Helsinki"]

# A, B, C, D, E
print "Parts A, B, C, D, E"
print len(mylist)
print mylist[2]
print mylist[1:]
print mylist[-1]
print mylist.index("Cairo")
print ""

# F
print "Part F"
part_f = mylist
print part_f
print part_f.pop(1)
print part_f
print ""

# G
print "Part G"
part_g = mylist
print part_g
part_g.sort(reverse = True)
print part_g
print ""

# H
print "Part H"
part_h = mylist
print part_h
part_h.append("Berlin")
print part_h




And my results:

Parts A, B, C, D, E
5
Cairo
['Barcelona', 'Cairo', 'Florence', 'Helsinki']
Helsinki
2

Part F
['Athens', 'Barcelona', 'Cairo', 'Florence', 'Helsinki']
Barcelona
['Athens', 'Cairo', 'Florence', 'Helsinki']

Part G
['Athens', 'Cairo', 'Florence', 'Helsinki']
['Helsinki', 'Florence', 'Cairo', 'Athens']
Part H
['Helsinki', 'Florence', 'Cairo', 'Athens']


It seems that even though I tried to make a new variable in the parts that will change the list it still changes mylist and I would like to know why?

I'm using Python 2.7.2

Thank you in advance
morsebl
 
Posts: 2
Joined: Fri Jul 26, 2013 2:52 pm

Re: Hi, I'm new to Python

Postby Yoriz » Fri Jul 26, 2013 3:53 pm

Code: Select all
mylist = ["Athens", "Barcelona", "Cairo", "Florence", "Helsinki"]


part_f = mylist  # this just make's part_f point to the same object as mylist
print part_f is mylist

part_g = mylist[:]  # this makes part_g point to a copy of mylist
print part_g is mylist

print id(mylist), id(part_f), id(part_g)  # notice how mylist & part_f have
#                                           the same id

Output
Code: Select all
True
False
25202520 25202520 25219520
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Hi, I'm new to Python

Postby morsebl » Fri Jul 26, 2013 3:58 pm

Okay, I was thinking something like that was happening but wanted to be sure.

Thanks for the speedy response! I'm sure there will probably be more questions from me as I go through this book.

Thanks again
morsebl
 
Posts: 2
Joined: Fri Jul 26, 2013 2:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: nischalinn and 4 guests