Python数据分析(中英对照)·Lists 列表

作者 : jamin 本文共6308个字,预计阅读时间需要16分钟 发布时间: 2020-10-18 共1102人阅读

1.2.2: Lists 列表

列表是任何类型的对象的可变序列。
Lists are mutable sequences of objects of any type.
它们通常用于存储同质项目。
And they’re typically used to store homogeneous items.
列表是序列的一种类型,就像字符串一样,但它们确实有区别。
Lists are one type of sequence, just like strings but they do have their differences.
如果我们比较字符串和列表,一个区别是字符串是单个字符的序列,
If we compare a string and a list, one difference is that strings are sequences of individual characters,
而列表是任何类型Python对象的序列。
whereas lists are sequences of any type of Python objects.
字符串和列表之间的另一个区别是字符串是不可变的,而列表是可变的。
Another difference between strings and lists is that strings are immutable, whereas lists are mutable.
除了这两个区别之外,字符串和列表当然也有自己的方法。
In addition to these two differences, strings and lists, of course,come with their own methods.
通常情况下,列表只包含一种类型的对象,尽管这不是严格的要求。
It is common practice for a list to hold objects of just one type,although this is not strictly a requirement.
让我们尝试几个简单的列表来测试它们。
Let’s try a couple of simple lists to experiment with them.
让我们构造一个简单的数字列表,以进一步了解列表。
Let’s construct a simple list of numbers to learn a little bit more about lists.
所以我要构造一个数字列表。
So I’m going to construct a list of numbers.
我要称之为数字。
I’m going to call it numbers.
我将使用数字2、4、6和8。
And I’ll use numbers 2, 4, 6, and 8.
假设我想提取或访问列表中的第一个元素。
Imagine I wanted to extract, or access, the first element of my list.
我要做的第一件事是键入列表的名称,然后我需要方括号。
The first thing for me to do is type the name of the list,then I need my square brackets.
现在请记住,在Python中,索引从零开始。
Now remember, in Python, indexes start at zero.
因此,为了能够查看该列表的第一个元素,我需要将其放入索引0,位置0。
So for me to be able to look at the first element of that list,I need to put in index 0, position 0.
在这里,Python告诉我第一个对象,即位于位置0的对象,是数字2。
Here, Python tells me that the first object, meaning the object located at position 0, is number 2.
如果我将索引更改为1,Python将给我第二个对象。
If I change the index to 1, Python gives me the second object.
现在,如果我想知道列表上最后一个对象是什么,我可以从右到左计算位置。
Now if I wanted to find out what is the very last object on my list,I can count positions from right to left.
这意味着我必须使用负指数。
And that means I have to use negative indices.
因此,如果我键入数字减1,Python告诉我列表中最后一个元素是数字8。
So if I type numbers minus 1, Python tells me the very last element of my list is the number 8.
记住,因为列表是可变序列,所以我们可以在创建它们之后修改它们的内容。
Remember, because lists are mutable sequences, we can modify their content and after we’ve created them.
让我们试着在列表的末尾添加一个新的数字,一个数字10。
Let’s try appending a new number, a number 10, to the end of our list.
要做到这一点,我首先键入数字,然后需要点,然后键入append。
To do that, the first thing I type is numbers, then I need my dot,and I type append.
在括号里,我输入了数字10。
And inside the parentheses, I put in the number 10.
如果我现在要求Python显示列表的内容,我将看到数字10已附加到该列表中。
If I now ask Python to show the content of the list,I’ll see that number 10 has been appended to that list.
我们通常希望执行的另一个操作是将两个或多个列表连接在一起。
Another operation we commonly would like to do is to concatenate two or more lists together.
现在考虑一个情况,我有另一个列表。
Now consider a situation where I have another list.
我们就叫它x吧。
Let’s just call that x.
让我们假设新的列表有数字12、14和16。
And let’s say the new list has numbers 12, 14, and 16.
在Python中,只要两个对象都是列表,我就可以使用加号来连接它们。
In Python, I can use the plus sign as long as both objects are lists to concatenate them.
所以我可以输入数字加x。
So I can type numbers plus x.
你可以看到结果是另一个列表。
And you see that the result is another list.
我可以问一下,Python最近输出的对象是什么类型的?
I can check that by asking, what is the type of the object that Python most recently outputted?
这是一个列表。
And that’s a list.
因此,当我键入一个列表和另一个列表时,我将这两个列表连接在一起。
So when I type a list plus another list, I’m concatenating these two lists together.
现在,让我们更详细地了解两种列表方法。
Let’s now look at a couple of list methods in a little bit more detail.
在这种情况下,我们有一个数字列表。
In this situation, we have a numbers list.
我想做的是,我想反转列表的内容。
And what I would like to do is, I would like to reverse the content of the list.
所以我想把我的最后一个物体放在第一位,第二个物体放在第二位,以此类推。
So I would like to have my last object first, second to last object second,and so on.
在这种情况下,我可以使用反向方法。
In this case, I can use the reverse method.
所以我可以输入数字,我可以调用反向方法。
So I can type numbers, and I can call the reverse method.
但是请看,Python不会向我返回任何对象。
But see, Python doesn’t return any object to me.
这是因为列表方法就是所谓的就地方法。
This is because list methods are what are called in-place methods.
他们修改了原始列表。
They modify the original list.
为了了解这一点,我现在可以输入数字,我可以看到列表上的数字已经颠倒了。
To see that, I can now type numbers, and I can see that the numbers on the list have been reversed.
这就是发生的事情。
So this is what happens.
如果我们这里有一个列表,我们有我们的数字1,3,等等。
If we have a list here – we have our numbers 1, 3, and so on.
我们最后的号码是23。
Our last number is 23.
反向方法的作用如下:
What the reverse method does is the following:
它在我的列表上运行,意思是原始列表。
It operates on the list that I have, meaning the original list.
它首先移动最后一个对象,最后移动第一个对象。
It moves the last object first and the first object last.
然后将第二个到最后一个对象移动到第二个位置,依此类推。
Then it takes the second to last object, moves that to the second position,and so on.
因此,结果是列表中对象的顺序到最后将被颠倒。
So the consequence is that the ordering of the objects in the list will have been reversed by the end.
然后让我们看一下排序方法。
Let’s then a look at the sort method.
考虑一个名为名字的列表,我们有四个名字,ZoFiA,亚历克斯,摩根和安东尼。
Consider a list called names where we have four names, Zofia, Alex,Morgan, and Anthony.
列表方法之一称为排序方法。
One of the list methods is called the sort method.
如果我键入names.sort并在sort的末尾加上括号,我就可以对该列表的内容进行排序。
If I type names.sort and place parentheses at the end of sort,I can sort the contents of this list.
如果我现在键入名称,您将看到名称已按字母顺序排序。
If I now type names, you’ll see that names have been sorted to be in alphabetical order.
因此,列表中对象的新顺序是Alex、Anthony、Morgan和Zofia。
So the new ordering of objects in the list is Alex, Anthony, Morgan, and Zofia.
现在Python中还有另一个函数叫做“sorted”。
Now there is another function in Python which is called "sorted".
函数的运行方式有点不同。
And the way that function operates is a little bit different.
当我们使用list方法sort时,我们将获取现有列表并重新排序该列表中的对象。
When we’re using the list method sort, we’re taking the existing list and reordering the objects within that list.
如果我们使用sorted函数,我们实际上是在要求Python构造一个全新的列表。
If we’re using the sorted function, we’re actually asking Python to construct a completely new list.
它将使用前一个列表中的对象构造此新列表,以使新列表中的对象
It will construct this new list using the objects in the previous list in such a way that the objects in the new list
将按排序顺序显示。
will appear in a sorted order.
让我们看看这是怎么发生的。
So let’s see how this happens.
让我看看我的名单,让我把它颠倒过来。
Let me take my names list, and let me just reverse that.
为了确认发生了什么,现在订单已经如我们预期的那样被翻转了。
And to confirm what has happened, now the ordering has been flipped as we would expect.
然后我可以说一些类似于排序的名称,这将是一个新对象。
I can then say something like sorted_names,which would be a new object.
我使用排序函数,并将其应用于我的列表名。
I use the sorted function and I apply it to my list names.
现在如果我看到名字的内容,你会看到什么都没有发生。
Now if I see what is the content of names,you see that nothing has happened.
那是因为我没有调用任何列表方法。
That’s because I haven’t called any list method.
但是如果我看一下排序的名称的内容,你会发现它的对象与原始列表名称完全相同,
But if I look at the contents of sorted_names,you’ll see that it has the same exact objects as the original list names,
但在这种情况下,它们是按字母顺序排序的。
but in this case, they have been alphabetically sorted.
最后,如果您想知道列表包含多少对象,我们可以使用一个通用的序列函数len。
Finally, if you wanted to find out how many objects our list contains,we can use a generic sequence function, len.
因此,我们可以键入len(names),Python告诉我们,我们的列表包含四个对象。
So we can type len(names), and Python tells us that our list contains four objects.

本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络 » Python数据分析(中英对照)·Lists 列表

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用

提供最优质的资源集合

立即加入 友好社区
×