列表和元组 [英] Lists and Tuples

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

问题描述

我大部分时间都在玩清单和元组,以便很好地掌握你可以用它们做什么。我仍然有一个问题,那就是,你什么时候应该选择一个列表或者一个元组?
元组?我知道一个元组是不可变的,一个列表是可变的但是必须要有更多它才能比它更多。我用列表尝试的所有内容都与元组一样。那么,什么是

的差异以及为什么选择其中一个?


杰克

I''ve spent most of the day playing around with lists and tuples to get a really good grasp on what
you can do with them. I am still left with a question and that is, when should you choose a list or
a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it
than just that. Everything I tried with a list worked the same with a tuple. So, what''s the
difference and why choose one over the other?

Jeff

推荐答案

Jeff Wagner< JW ***** @ hotmail.com>写道:
Jeff Wagner <JW*****@hotmail.com> writes:
我花了大部分时间来处理列表和元组,以便很好地掌握你可以用它们做些什么。我仍然有一个问题,那就是,你什么时候应该选择列表或
元组?我知道元组是不可变的,列表是可变的
但是必须有更多的东西。我尝试的所有内容与列表一样,与元组一样。那么,有什么区别
以及为什么选择其中一个?
I''ve spent most of the day playing around with lists and tuples to
get a really good grasp on what you can do with them. I am still
left with a question and that is, when should you choose a list or a
tuple? I understand that a tuple is immutable and a list is mutable
but there has to be more to it than just that. Everything I tried
with a list worked the same with a tuple. So, what''s the difference
and why choose one over the other?



试试这个列表:


a = [1,2,3,4,5]

a [3] = 27

打印一份


然后试试吧一个元组。


Try this with a list:

a = [1, 2, 3, 4, 5]
a[3] = 27
print a

Then try it with a tuple.


文章< 7x ************ @ ruckus.brouhaha.com>,

Paul Rubin< http://ph****@NOSPAM.invalid>写道:
In article <7x************@ruckus.brouhaha.com>,
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
尝试使用列表:

a = [1,2,3,4,5]
a [3] = 27
打印
然后用元组尝试。
Try this with a list:

a = [1, 2, 3, 4, 5]
a[3] = 27
print a

Then try it with a tuple.




这是真的,但另一个答案是:你应该简单地使用元组

不同项目的序列(比如函数的参数)。你需要使用列表来获得类似物品的更长序列。


-

David Eppstein http://www.ics.uci.edu/~eppstein/

大学加州,欧文,信息学院和计算机科学



That''s true, but another answer is: you should use tuples for short
sequences of diverse items (like the arguments to a function). You
should use lists for longer sequences of similar items.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science


2003年12月4日21:31:12 -0800,Paul Rubin< http://ph****@NOSPAM.invalid> wrotf:
On 04 Dec 2003 21:31:12 -0800, Paul Rubin <http://ph****@NOSPAM.invalid> wrotf:
Jeff Wagner< JW ***** @ hotmail.com>写道:
Jeff Wagner <JW*****@hotmail.com> writes:
我花了大部分时间来处理列表和元组,以便很好地掌握你可以用它们做些什么。我仍然有一个问题,那就是,你什么时候应该选择列表或
元组?我知道元组是不可变的,列表是可变的
但是必须有更多的东西。我尝试的所有内容与列表一样,与元组一样。那么,有什么区别
以及为什么选择其中一个?
I''ve spent most of the day playing around with lists and tuples to
get a really good grasp on what you can do with them. I am still
left with a question and that is, when should you choose a list or a
tuple? I understand that a tuple is immutable and a list is mutable
but there has to be more to it than just that. Everything I tried
with a list worked the same with a tuple. So, what''s the difference
and why choose one over the other?



尝试使用列表:

a = [1 ,2,3,4,5]
a [3] = 27
然后用元组尝试。


Try this with a list:

a = [1, 2, 3, 4, 5]
a[3] = 27
print a

Then try it with a tuple.




这是因为元组是不可变的,列表是可变的,但还有什么?我想我说的所有我用b -le
尝试了一个列表...没有提到我没有试图打破不可变/可变

规则我是意识到。除了尝试更改元组之外,我可以将它切割,切片和切块,就像我可以列出一样。他们似乎也有相同的内置方法。


从我所看到的,我没有理由想要使用元组,我认为有

我缺少的东西。为什么Guido会尽一切努力包含元组if(如图所示)

列表同样好但更强大......你可以更改列表的内容。


杰夫



That''s because a tuple is immutable and a list is mutable but what else? I guess I said everything I
tried with a tuple worked with a list ... not mentioning I didn''t try to break the immutable/mutable
rule I was aware of. Besides trying to change a tuple, I could cut it, slice and dice it just like I
could a list. They seemed to have the same built-in methods, too.

From what I can see, there is no reason for me to ever want to use a tuple and I think there is
something I am missing. Why would Guido go to all the effort to include tuples if (as it appears)
lists are just as good but more powerful ... you can change the contents of a list.

Jeff


这篇关于列表和元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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