P: n/a
|
i m trying to reverse the order in which the
strings are stored in list
then pop it into another list
what m i doin worng??
here's the code:
list1 = []
list2 = []
list1.extend('123456789')
print list1
for a in list1:
list2 = list1.pop()
print list2
--
* Posted with NewsLeecher v3.0 Beta 7
* http://www.newsleecher.com/?usenet | |
Share this Question
P: n/a
| bl**@blah.blah wrote: i m trying to reverse the order in which the strings are stored in list
then pop it into another list
what m i doin worng??
here's the code:
list1 = [] list2 = [] list1.extend('123456789')
print list1
for a in list1: list2 = list1.pop()
print list2 -- * Posted with NewsLeecher v3.0 Beta 7 * http://www.newsleecher.com/?usenet
well, mant things are happening, so many it's not clear to me what
you're trying to do...
BUT, considering your for statement, since you are poping elements out
of list you are shortening it, so you are only getting as far as
element '5'
If what you want is a reversed copy, you could just append list1
elements to list2, and use the reverse function such as ... for i in list1:
.... list2.append(i)
.... list2.reverse() list1
['1', '2', '3', '4', '5', '6', '7', '8', '9'] list2
['9', '8', '7', '6', '5', '4', '3', '2', '1'] | |
P: n/a
|
Here is an example of copying then reversing a list: l1 = [1,2,"C"] l2 = l1[:] l2.reverse() l2
['C', 2, 1] | |
P: n/a
| jm*********@gmail.com wrote: If what you want is a reversed copy, you could just append list1 elements to list2, and use the reverse function such as ... for i in list1: ... list2.append(i) ... list2.reverse() list1 ['1', '2', '3', '4', '5', '6', '7', '8', '9'] list2 ['9', '8', '7', '6', '5', '4', '3', '2', '1']
It's much clearer and simpler to just do: list2 = list1[:] list2.reverse()
This saves all the messing around with append, and has the advantage
that it runs faster (by a factor of 30, on my computer).
-- David | |
P: n/a
| jm*********@gmail.com writes: If what you want is a reversed copy, you could just append list1 elements to list2, and use the reverse function such as ... for i in list1: ... list2.append(i) ...
Don't do this by ahnd - let python do it for you:
list2 = list(list1)
or
list2 = list1[:]
<mike list2.reverse() list1 ['1', '2', '3', '4', '5', '6', '7', '8', '9'] list2
['9', '8', '7', '6', '5', '4', '3', '2', '1']
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. | |
P: n/a
|
....
list2=list1[:]
....
Yes, naturally, where was my head ?-)) | |
P: n/a
| bl**@blah.blah wrote: i m trying to reverse the order in which the strings are stored in list
then pop it into another list
what m i doin worng??
here's the code:
list1 = [] list2 = [] list1.extend('123456789')
How about this instead (Python 2.4 or later):
list2 = list(reversed('123456789'))
-Peter | |
P: n/a
|
Thanks Guys, Wow, i didnt knew that there was alist reverse function.
thx. also. i need a good documentation of the os and sys modules as
well as builtin functions of python.
the standard python documentation doesnt work for me. can u recommend
something?
--
* Posted with NewsLeecher v3.0 Beta 7
* http://www.newsleecher.com/?usenet | |
P: n/a
| bl**@blah.blah <blahman> wrote: Thanks Guys, Wow, i didnt knew that there was alist reverse function. thx. also. i need a good documentation of the os and sys modules as well as builtin functions of python.
the standard python documentation doesnt work for me. can u recommend something?
I'm biased, but I'd recommend the book "Python in a Nutshell". My
suggestion: take advantage of the "Safari" online library for 2 weeks'
of free subscription -- make sure you cancel after 13 days if you want
to avoid being billed for the subscription's fee. In those 2 weeks you
get to read for free a few books in the online library -- I'd suggest
sampling the Nutshell, the Cookbook and possibly a couple more...
Alex | | This discussion thread is closed Replies have been disabled for this discussion. | | Question stats - viewed: 1216
- replies: 9
- date asked: Nov 3 '05
|