[Tutor] Soem list operation questions?
Guillermo Fernandez Castellanos
guillermo.fernandez.castellanos at gmail.com
Tue Dec 28 05:19:35 CET 2004
hi!
> 1. Is there any easy way to compeltely reverse a list so that the last index
> becomes the first, etc.?
The doc is your friend :-)
http://www.python.org/dev/doc/maint24/lib/typesseq-mutable.html
"""s.reverse() reverses the items of s in place"""
>>> a=[1,2,3,4]
>>> a.reverse()
>>> a
[4, 3, 2, 1]
> 2. Is there any way to take seperate integers in a list and combine them
> into digits of one number? (e.g. changing [1,2,3,4] into 1234)
Mmm...
Well, there is a way:
lambda l: int("".join([str(x) for x in l]))
>>> a=[1,2,3,4]
>>> c=lambda l: int("".join([str(x) for x in l]))
>>> c(a)
1234
Let's have a more detailed of what I do by writting the non-ofuscated
equivalent code:
def c(l):
# Converting all elements of the list into strings
a=[str(x) for x in l]
# Joigning all those strings into a single string
s="".join(a)
# Converting the new string into an integer
int(s)
There's probably a better way to do it, but in the meantime you can
start using this.
Enjoy!
Guille
More information about the Tutor
mailing list