在传递需要列表或元组的参数时传递什么? [英] What to pass when passing arguments where a list or tuple is required?

查看:154
本文介绍了在传递需要列表或元组的参数时传递什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用以下哪项和为什么?

  import numpy as np 
a = np.zeros [2,3])
b = np.zeros((2,3))

在很多情况下,你可以通过任何一种方式传递参数,我只是想知道一个是更多的Pythonic,还是有其他原因,一个应该优先于另一个。



我看了一下这个问题人们试图解释元组和列表之间的区别。这不是我感兴趣的,除非有理由我应该关心,我当然忽略了!



更新:



虽然使用numpy作为例子,但是这通常适用于python。非numpy示例如下:

  a = max([1,2,3,3,4,5] $ bb = max((1,2,3,5,4))

因为一些答案在解释中使用numpy

解决方案

我回答这个问题,一个构造函数或函数,超过这个类型并不重要。如果你需要传入一个hashable参数,你需要一个元组。如果你需要它的变异,传递一个列表(这样你不添加元组到元组,从而乘以对象的创建。)



问题是,更好的选择在情境上变化。这里是权衡。



list 类型开始,它是可变的,它为未来的扩展预分配内存:

  a = np.zeros([2,3])

$ b

:这会浪费内存,



接下来, tuple 类型是不可变的。它不需要预先分配内存以供将来扩展,因为它不能扩展。

  b = np.zeros((2,3))

Pro :它使用最少的内存,而且性能更高。



strong> Con :可读性稍差。



我的首选项是传递元组文字,将被许多人使用。另一方面,当我使用交互式解释器时,我喜欢传递列表,因为它们更容易阅读,方括号和括号之间的对比使得可以方便地进行视觉解析。



您应该只关心函数中的性能,其中代码编译为字节码:

  >>>> min(timeit.repeat('foo()','def foo():return(0,1)'))
0.080030765042010898
>> min(timeit.repeat('foo()','def foo():return [0,1]'))
0.17389221549683498

最后,请注意,性能考虑将与其他考虑因素相悖。你使用Python的开发速度,而不是速度的算法实现。如果你使用错误的算法,你的性能会更糟。它也在许多方面表现很好。我认为这只是重要的,因为它可以缩放,如果它可以改善严重使用的过程死于一千个切割的死亡。


Which of the following should I use and why?

import numpy as np
a = np.zeros([2, 3])
b = np.zeros((2, 3))

There are many cases where you can pass arguments in either way, I just wonder if one is more Pythonic or if there are other reasons where one should be preferred over the other.

I looked at this question where people tried to explain the difference between a tuple and a list. That's not what I'm interested in, unless there are reasons I should care which I ignore of course!

UPDATE:

Although numpy was used as an example this pertains generally to python. A non numpy example is as follows:

a = max([1, 2, 3, 5, 4])
b = max((1, 2, 3, 5, 4))

I'm not editing the above because some answers use numpy in their explanation

解决方案

I'm answering this in the context of passing a literal iterable to a constructor or function beyond which the type does not matter. If you need to pass in a hashable argument, you need a tuple. If you'll need it mutated, pass in a list (so that you don't add tuples to tuples thereby multiplying the creation of objects.)

The answer to your question is that the better option varies situationally. Here's the tradeoffs.

Starting with list type, which is mutable, it preallocates memory for future extension:

a = np.zeros([2, 3])

Pro: It's easily readable.

Con: It wastes memory, and it's less performant.

Next, the tuple type, which is immutable. It doesn't need to preallocate memory for future extension, because it can't be extended.

b = np.zeros((2, 3))

Pro: It uses minimal memory, and it's more performant.

Con: It's a little less readable.

My preference is to pass tuple literals where memory is a consideration, for example, long-running scripts that will be used by lots of people. On the other hand, when I'm using an interactive interpreter, I prefer to pass lists because they're a bit more readable, the contrast between the square brackets and the parenthesis makes for easy visual parsing.

You should only care about performance in a function, where the code is compiled to bytecode:

>>> min(timeit.repeat('foo()', 'def foo(): return (0, 1)'))
0.080030765042010898
>>> min(timeit.repeat('foo()', 'def foo(): return [0, 1]'))
0.17389221549683498

Finally, note that the performance consideration will be dwarfed by other considerations. You use Python for speed of development, not for speed of algorithmic implementation. If you use a bad algorithm, your performance will be much worse. It's also very performant in many respects as well. I consider this only important insomuch as it may scale, if it can ameliorate heavily used processes from dying a death of a thousand cuts.

这篇关于在传递需要列表或元组的参数时传递什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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