Suppose i have a big list and i want to take tke the first one and rest
of the list like car/cdr in lisp.
is there any easy way to do this in python?
Only way i know is
a = range(10)
x, y = a[0], a[1:]
In perl, it is possible to do multiple assignment like this....
@a = (1, 2, 3);
($x, @y) = @a;
I find this is a good feature and missing in python.
Why can't python support something like this:
x, *y = a
* operator is used in the usual sence here.
This is not really a new concept to python, infact when calling a
function which takes variable arguments, it is used in a similar way.
for example:
def f(x, *y): print x, y
f(1, 2, 3)
the argument passing here is very similar to the multiple assignment x,
*y = (1, 2, 3)
any comments?
-anand 8 1738
Anand schrieb: Suppose i have a big list and i want to take tke the first one and rest of the list like car/cdr in lisp. is there any easy way to do this in python?
Only way i know is
a = range(10) x, y = a[0], a[1:]
You have so many higher-level ways to access and iterate through lists
in Python, that you normally just don't need to do things like that.
Also, in the frequent case where y=a, you can just write x = a.pop(0).
Why can't python support something like this:
x, *y = a
This is not really a new concept to python, infact when calling a function which takes variable arguments, it is used in a similar way.
You're right, that would not be so far off.
But then, the following should be also supported:
*x, y = a # x, y = a[:-1], y = a[-1]
x, *y, z = a # x, y, z = a[0], a[1:-1], a[-1]
Of course, there can be only one variable with an asterisk.
(But note that in the situation of a function taking parameters, that
variable must always be the last.)
But I don't know if this is really useful enough...
-- Christoph
Christoph Zwerschke wrote: You're right, that would not be so far off. But then, the following should be also supported:
*x, y = a # x, y = a[:-1], y = a[-1] x, *y, z = a # x, y, z = a[0], a[1:-1], a[-1]
Of course, there can be only one variable with an asterisk. (But note that in the situation of a function taking parameters, that variable must always be the last.)
But I don't know if this is really useful enough...
things like this are proposed from time to time (I haven't looked, but
there might even be a PEP somewhere). however, function calls and
assignments are two different things, and I'm not convinced that it's
not yet another hypergeneralization...
</F>
> You're right, that would not be so far off. But then, the following should be also supported:
*x, y = a # x, y = a[:-1], y = a[-1] x, *y, z = a # x, y, z = a[0], a[1:-1], a[-1]
Of course, there can be only one variable with an asterisk. (But note that in the situation of a function taking parameters, that variable must always be the last.)
Same argument can be applied for functions also. whats wrong in having
some thing like this?
def f(x, *y, z): pass
I think there is a problem in both these cases.
But I don't know if this is really useful enough...
I think it is really useful.
One which i encountered was there is a file where each line has tokens
separated by commas. First token is the id and i want to use it in a
special way.
Wouldn't it be nice to say
id, *tokens = line.split(',')
than
tokens = line.split(',')
id = tokens.pop(0)
- anand
Anand wrote: Wouldn't it be nice to say
id, *tokens = line.split(',')
id, tokens_str = line.split(',', 1)
STeVe
>> Wouldn't it be nice to say id, *tokens = line.split(',')
id, tokens_str = line.split(',', 1)
But then you have to split tokens_str again.
id, tokens_str = line.split(',', 1)
tokens = tokens_str.split(',')
this is too verbose.
anand
Anand wrote: Wouldn't it be nice to say id, *tokens = line.split(',')
id, tokens_str = line.split(',', 1)
But then you have to split tokens_str again.
id, tokens_str = line.split(',', 1) tokens = tokens_str.split(',')
Sorry, it wasn't clear that you needed the tokens from the original post.
If you're interested in this, the best route is to write up a PEP and
provide an implementation. The new AST in Python makes this kind of
thing a bit easier.
Steve
Anand wrote: Suppose i have a big list and i want to take tke the first one and rest of the list like car/cdr in lisp. is there any easy way to do this in python?
It seems like overkill to me to make the syntax more
complex just to avoid writing a one-line function.
def first_rest(seq): return seq[0], seq[1:]
Anand wrote: Wouldn't it be nice to say id, *tokens = line.split(',')
id, tokens_str = line.split(',', 1)
But then you have to split tokens_str again.
id, tokens_str = line.split(',', 1) tokens = tokens_str.split(',')
this is too verbose.
head_tail = lambda seq: seq[0], seq[1:]
....
id, tokens = head_tail(line.split(','))
but I do like the syntax you're proposing !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])" This discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by AMT2K5 |
last post: by
|
5 posts
views
Thread by vj |
last post: by
|
6 posts
views
Thread by Suresh Jeevanandam |
last post: by
|
77 posts
views
Thread by berns |
last post: by
|
22 posts
views
Thread by tshad |
last post: by
|
21 posts
views
Thread by vlsidesign |
last post: by
| | | | | | | | | | | |