老鼠! vararg作业不起作用 [英] Rats! vararg assignments don't work

查看:70
本文介绍了老鼠! vararg作业不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,所以请耐心等待。看到

后varargs如何在参数列表中工作,如下所示:

def func(x,* arglist):

和this:

x = func(1,* moreargs)

我以为我会试试这个:

first,* rest = arglist

不用说,它没有用。这给我留下两个问题。


首先,有一个很好的方法吗?现在,我正在使用它:

优先,休息= arglist [0],arglist [1:]

但它在我嘴里留下了不好的味道。


其次,有什么理由不行吗?看起来像是一个明显的成语,我不敢相信我是第一个提出这个想法的人。我现在没有时间去寻找潜水源,所以我无法判断实施效率是否非常低效。


谢谢!

I''m a relative newbie to Python, so please bear with me. After seeing
how varargs work in parameter lists, like this:
def func(x, *arglist):
and this:
x = func(1, *moreargs)
I thought that I''d try this:
first, *rest = arglist
Needless to say, it didn''t work. That leaves me with two questions.

First, is there a good way to do this? For now, I''m using this:
first, rest = arglist[0], arglist[1:]
but it leaves a bad taste in my mouth.

Second, is there any good reason why it shouldn''t work? It seems like
such an obvious idiom that I can''t believe that I''m the first to come up
with the idea. I don''t really have the time right now to go source
diving, so I can''t tell if it would be wildly inefficient to implement.

Thanks!

推荐答案

您的attemtp:

Your attemtp:

展开 | 选择 | Wrap | 行号


5月29日晚上11点33分,Matimus< mccre ... @ gmail.com写道:
On May 29, 11:33 pm, Matimus <mccre...@gmail.comwrote:

你的attemtp:

Your attemtp:

展开 | 选择 | Wrap | 行号


samwyse写道:
samwyse wrote:

我是Python的相对新手,所以请耐心等待。看到

后varargs如何在参数列表中工作,如下所示:

def func(x,* arglist):

和this:

x = func(1,* moreargs)

我以为我会试试这个:

first,* rest = arglist

不用说,它没有用。这给我留下两个问题。


首先,有一个很好的方法吗?现在,我正在使用它:

优先,休息= arglist [0],arglist [1:]

但它在我嘴里留下了不好的味道。
I''m a relative newbie to Python, so please bear with me. After seeing
how varargs work in parameter lists, like this:
def func(x, *arglist):
and this:
x = func(1, *moreargs)
I thought that I''d try this:
first, *rest = arglist
Needless to say, it didn''t work. That leaves me with two questions.

First, is there a good way to do this? For now, I''m using this:
first, rest = arglist[0], arglist[1:]
but it leaves a bad taste in my mouth.



嗯,你的moreargs参数是一个元组,并且有无数的方法来处理元组。

来处理一个元组。 (如果你把它转换成一个列表,就会更多。)


如果你只想提取第一个arg,那么你的

代码是相当的Python的。但是,如果你要在一个循环

中连续处理每个arg,你有几个更好的选择:


例如:

for moreargs中的arg:#循环遍历每个arg

<用arg做一些事情>





for i in range(len(moreargs)):

<用morergs做一些事[i]#Extract iith arg


或者


argslist = list(moreargs)

而argslist:

firstarg = argslist.pop(0)#Extract first arg

<用firstarg做点什么>


Gary Herron

Well, your moreargs parameter is a tuple, and there are innumerable ways
to process a tuple. (And even more if you convert it to a list.)

If you are just interested in extracting only the first arg, then your
code is quite Pythonic. However, if you are going to do that in a loop
to successively process each arg, the you have several better options:

For instance:
for arg in moreargs: # Loop through each arg
<do something with arg>

or

for i in range(len(moreargs)):
<do something with morergs[i] # Extract ith arg

or

argslist = list(moreargs)
while argslist:
firstarg = argslist.pop(0) # Extract first arg
<do something with firstarg>

Gary Herron


其次,有没有它不应该工作的充分理由?看起来像是一个明显的成语,我不敢相信我是第一个提出这个想法的人。我现在没有时间去寻找潜水源,所以我无法判断实施效率是否非常低效。


谢谢!
Second, is there any good reason why it shouldn''t work? It seems like
such an obvious idiom that I can''t believe that I''m the first to come up
with the idea. I don''t really have the time right now to go source
diving, so I can''t tell if it would be wildly inefficient to implement.

Thanks!


这篇关于老鼠! vararg作业不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆