列表和元组以及更多 [英] Lists and Tuples and Much More

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

问题描述

我将开始在一个帖子中分组我的所有问题,因为这是我今天的第二个
秒,而sorta让我感到愚蠢到不得不打扰你们所有人

有琐碎的问题。我只会把我的问题分开:

-------------------------------- -------------------------------------------------- ---------

现在讨论这个问题:

列出'和'元组'

我不喜欢看不出两者之间的区别。我的意思是,我明白一个

列表是可变的,一个元组是不可变的。

我不了解它们的是除此之外,分离

这两个。我做了一些尝试,试图更好地理解它,但是

只会让我更加困惑。


列表如下:

I''m going to start grouping all my questions in one post as this is my
second today, and sorta makes me feel dumb to keep having to bother you all
with trivial questions. I''ll just seperate my questions with:
-------------------------------------------------------------------------------------------
Now onto the issue:
List''s and Tuple''s
I don''t see the distinction between the two. I mean, I understand that a
list is mutable and a tuple is immutable.
The thing that I dont understand about them is what, besides that, seperates
the two. I did a little experimentation to try to understand it better, but
only confused myelf more.

A list looks like this:


>>> my_list = [1,2,3,4,5,6]
>>>my_list = [1, 2, 3, 4, 5, 6]



和元组如下所示:

and a tuple looks like this:


>>> my_tuple =(1,2,3,4,5,6)
>>>my_tuple = (1, 2, 3, 4, 5, 6)



现在你可以添加到列表中,但不能添加到元组中:

Now you can add to a list, but not a tuple so:


>>> my_list.append(my_tuple)#or延伸到这个问题吧?
>>>my_list.append(my_tuple) #or extend for that matter right?



[1,2,3,4,5,6,(1,2,3,4,5,6) ]


这是非常准确吗?哪个更好的资源....我是

猜测一个元组,因为它没有改变。


和最后一个例子提出另一个问题。什么是与

tupple有什么交易,其中有一个列表如下:

[1, 2, 3, 4, 5, 6, (1, 2, 3, 4, 5, 6)]

Is that pretty much accurate? And which is better on resources....I''m
guessing a tuple seeing as it doesn''t change.

And the last example brings up another question. What''s the deal with a
tupple that has a list in it such as:


>>> my_tupple =(1,2,3,4,5,[6,7,8,9])
>>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])



现在我读到某个地方你可以改变那个tupple里面的列表。但是

我找不到任何描述如何做的文档。我能找到的关于这个问题的唯一的事情是,不要这样做,因为它比它更麻烦,而不是它的价值。但这对我来说无关紧要,因为我想知道

一切。

----------------- -------------------------------------------------- --------------------------


现在有追加。我在任何地方读到的只是添加'1元素

到列表的末尾。但如果你写:

Now I read somewhere that you could change the list inside that tupple. But
I can''t find any documentation that describes HOW to do it. The only things
I CAN find on the subject say, "Don''t do it because its more trouble than
it''s worth." But that doesn''t matter to me, because I want to know
everything.
---------------------------------------------------------------------------------------------

Now there comes append. I read everywhere that append only add''s 1 element
to the end of your list. But if you write:


>> my_list = [1,2,3, 4,5,6]
my_list.append([7,8,9,10])
my_list
>>my_list = [1, 2, 3, 4, 5, 6]
my_list.append([7, 8, 9, 10])
my_list



[1,2,3,4,5,6,[7,8,9,10]]


那是因为列表'',无论它们是什么包含,被计为1

元素?


您如何对列表中的列表进行排序?我想这与

结合上面的部分,但仍然是:

[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]

Is that because list''s, no matter what they contain, are counted as 1
element?

And how would you sort the list that''s in the list? I guess that goes in
conjunction with the section above, but still:


>> my_list = [6,4,3,5,2,1]
my_list.append([7,9,8,10])
my_list.sort()
my_list
>>my_list = [6, 4, 3, 5, 2, 1]
my_list.append([7, 9, 8, 10])
my_list.sort()
my_list



[1,2,3,4,5,6,[7,9,8,10] ]]


这又是我一无所获的东西。

-------------- -------------------------------------------------- -----------------------------


也许我只是不去看正确的地方。我唯一拥有的学习辅助工具是:这个新闻组; p, http:/ /diveintopython.org
http://python.org/ ,Beggining Python:从新手到专业,(现在不用笑)Python for Dummies。

[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]

This is, again, something I''m finding nothing on.
---------------------------------------------------------------------------------------------

Maybe I''m just not looking in the right spots. The only things I have as
learning aids are: this newsgroup ;p, http://diveintopython.org,
http://python.org/, Beggining Python: From Novice to Professional, and (now
don''t laugh) Python for Dummies.

推荐答案

Scott:


其他人会给你更多的答案,这里只是一点点。
Scott:

Others will give you many more answers, here is just a bit.

你会怎么排序?列表中的列表?我想这与

结合上面的部分,但仍然是:
And how would you sort the list that''s in the list? I guess that goes in
conjunction with the section above, but still:

>> my_list = [6,4,3,5,2,1]
my_list.append([7,9,8,10])
my_list.sort()
my_list
>>my_list = [6, 4, 3, 5, 2, 1]
my_list.append([7, 9, 8, 10])
my_list.sort()
my_list



[1,2,3,4,5,6,[7,9,8,10]]


[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]



在Python 3.0中这样的排序可能是不可能的(比较

列表与整数的顺序可能被视为毫无意义。否则你可以看到

单个数字作为len = 1的列表,就像另一种语言一样)。

Such sorting may be impossible in Python 3.0 (comparing the order of
lists with integers may be seen as meaningless. Otherwise you can see
single numbers as lists of len=1, like another language does).


这也是我一无所获的东西。
This is, again, something I''m finding nothing on.



也许是因为文档需要一些泛化功能。

列表是一个对象,它可以包含一系列对象。 />

再见,

熊宝宝

Maybe because documentation requires some generalization capabilities.
A list is a single object, and it can contain a sequence of objects.

Bye,
bearophile


现在我读到某处你可以改变
Now I read somewhere that you could change the

列在该tupple中。但我找不到任何描述如何做的

文档。
list inside that tupple. But I can''t find any
documentation that describes HOW to do it.



t =(1,2,[" red"," white"])

t [2] [1] =" ;紫色"

打印t

t [2]返回列表,所以第二行相当于:


lst = t [2]

lst [1] =" purple"


这正是您访问和更改内部列表的方式列表,

例如:


[1,2,[" red,white]]

t = (1, 2, ["red", "white"])
t[2][1] = "purple"
print t

t[2] returns the list, so the second line is equivalent to:

lst = t[2]
lst[1] = "purple"

That is exactly how you would access and change a list inside a list,
e.g.:

[1, 2, ["red", "white"]]


哪个资源更好....我猜一个元组看到

因为它没有变化。
And which is better on resources....I''m guessing a tuple seeing
as it doesn''t change.



我怀疑这很重要,除非你有几百万个列表v。

几百万元组。你需要知道元组的原因是

,因为它们是由一些内置函数返回的。另外,字典中的一个键可以是一个元组,但不是一个列表。但是在字典中使用元组作为

键可能是你永远不会做的事情。

I doubt it matters much unless you have several million lists v.
several million tuples. The reason you need to know about tuples is
because they are returned by some built in functions. Also, a key in
a dictionary can be a tuple, but not a list. But using a tuple as a
key in a dictionary is probably something you will never do.


现在有追加。我在任何地方都会阅读附加内容,只需在列表末尾添加'1

元素。但如果你写:
Now there comes append. I read everywhere that append only add''s 1
element to the end of your list. But if you write:


>> my_list = [1, 2,3,4,5,6]
my_list.append([7,8,9,10])
my_list _li
>>my_list = [1, 2, 3, 4, 5, 6]
my_list.append([7, 8, 9, 10])
my_list _li



[1,2,3,4,5,6,[7,8,9,10]]

[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]


这是因为列表',无论它们包含什么,都算作

1元素?
Is that because list''s, no matter what they contain, are counted as
1 element?



每个元素在列表中与索引位置相关联。当

你追加()到一个列表,你在列表中创建一个额外的索引位置

并指定你附加到该索引的东西

位置。你追加的东西成为列表中的一个元素,即

它与列表中的一个索引位置相关联。因此,当你追加()时,

,列表的长度只增加1:

Each "element" in a list is associated with an index position. When
you append() to a list you are creating one additional index position
in the list and assigning the thing you append to that index
position. The thing you append becomes one element in the list, i.e.
it''s associated with one index position in the list. As a result,
when you append(), the length of the list only increases by 1:


>> l = [1,2]
len(l)
>>l = [1, 2]
len(l)



2

2


>> a = [" red","白色"]
l.append(a)
l
>>a = ["red", "white"]
l.append(a)
l



[1,2,['' red'',''white'']]

[1, 2, [''red'', ''white'']]


>> len(l )
>>len(l)



3

3


>>>
>>>



您如何对列表中的列表进行排序?
And how would you sort the list that''s in the list?



通过召唤清单,然后对其进行排序:


t =(1,2,[" red" ,白色,ZZZ,abc])

t [2] .sort()

print t

By summoning up the list, and then sorting it:

t = (1, 2, ["red", "white", "ZZZ", "abc"])
t[2].sort()
print t


最后一个例子提出了另一个问题。什么是
And the last example brings up another question. What''s the deal with a

tupple的交易,其中包含一个列表:
tupple that has a list in it such as:

>> my_tupple =(1,2,3,4,5,[6,7,8,9])
>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])



现在我在某处读到你可以改变那个tupple里面的列表。但是

我找不到任何描述如何做的文档。我能找到的关于这个问题的唯一的事情是,不要这样做,因为它比它更麻烦,而不是它的价值。但这对我来说无关紧要,因为我想知道

一切。


Now I read somewhere that you could change the list inside that tupple. But
I can''t find any documentation that describes HOW to do it. The only things
I CAN find on the subject say, "Don''t do it because its more trouble than
it''s worth." But that doesn''t matter to me, because I want to know
everything.



您可以像这样更改元组内的列表:

You could change the list inside your tuple like this:


>> my_tupple =(1,2,3,4,5,[6,7,8,9])
my_tupple [5] .append(10)
my_tupple
>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])
my_tupple[5].append(10)
my_tupple



(1,2,3,4,5,[6,7,8,9,10] )

(1, 2, 3, 4, 5, [6, 7, 8, 9, 10])


现在有追加。我在任何地方读到的只是添加'1元素

到列表的末尾。但如果你写:
Now there comes append. I read everywhere that append only add''s 1 element
to the end of your list. But if you write:

> my_list = [1,2,3,4,5,6]
my_list.append([7,8,9,10])
my_list
>my_list = [1, 2, 3, 4, 5, 6]
my_list.append([7, 8, 9, 10])
my_list



[1,2,3,4,5, 6,[7,8,9,10]]


这是因为列表',无论它们包含什么,都算作1

元件?

[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]

Is that because list''s, no matter what they contain, are counted as 1
element?



是。

Yes.


您如何对列表中的列表进行排序?我想这与

结合上面的部分,但仍然是:
And how would you sort the list that''s in the list? I guess that goes in
conjunction with the section above, but still:

> my_list = [6,4,3,5,2,1]
my_list.append([7,9,8,10])
my_list.sort()
my_list
>my_list = [6, 4, 3, 5, 2, 1]
my_list.append([7, 9, 8, 10])
my_list.sort()
my_list



[1,2,3,4,5,6,[7,9,8,10]]

[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]



怎么样


>> my_list = [6,4,3,5,2,1]
my_list.append([7,9,8,10])
my_list [6] .sort()
my_list
>>my_list = [6, 4, 3, 5, 2, 1]
my_list.append([7, 9, 8, 10])
my_list[6].sort()
my_list



[6,4,3,5,2,1,[7,8,9,10]]
HTH,

Daniel

[6, 4, 3, 5, 2, 1, [7, 8, 9, 10]]
HTH,
Daniel


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

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