So this is a classic error. I've not made a copy of the list. I've got an alias of the list.
这是个相当经典的错误,在这里我并没有得到数组的。
I've thrown away most of the array- most of the list, I shouldn't say array- most of the list. All right?
我已经把数组的大部分都排除了-,列表的大部分-,我不应该说数组-列表的大部分?
If you ever try to go beyond the boundaries of an array, as we'll soon see, bad things happen.
如果你试着超出了数组的界限,我们将看到,会发生坏事情。
It is going to give me back a tuple a collection of two things, and so check out the syntax.
它将给我返回一个数组或者系列的一对值,然后检查一下语法。
I've written a little for loop, which is going to iterate over all of the elements in the list.
让我们先看看这儿的代码,我已经写了一个循环语句,用来迭代处理数组中所有的元素。
Yeah, it's kind of simple, but it gives me an ordered list of these things, And let's run it. OK.
让我们来运行运行吧,好,我会先去搜索一些数组中没有的元素,让我来试试,看-1在不在这个数组里。
And if I'm walking down the list, this is probably order of the length of the list s because I'm looking at each element once.
这可能大概就是数组的长度,因为我会遍历数组中的每个元素一次,现在你可能会想,等等,数组已经排好序了。
If I had lists of lists of lists, then it would only take out the first level of it.
贴到另外一个数组的后面,它当然是实现了套数组的操作。
When you are searching a list to see whether it has an element, you don't randomly probe the list, hoping to find whether or not it's there.
当我们在一个数组中,寻找目标元素的时候,我们不会随机的调查数组来看。
That's just a graphical incarnation of this thing called an array and notice, the games can, in fact, interact with a human much like Wednesday, I'm leaving my house.
它其实是数组变量的,图形化,具体化,这个游戏实际上是与人交互的,如周三看到的,我正要离开我的房子。
These are sort of the cells, if you like, in memory that are holding the elements of the list.
记忆就像是数组中的元素,我们之前说的是,我在这里开始并且比较。
I'm going to print it and let's see what it prints.
我要将其打出来,我想显示下数组的内容。
And again, this points out something I wanted to me-- I mentioned last time, list can be heterogeneous, in the sense that the elements can be multiple different types.
这儿又指出了一点我想要告诉大家的,我上节课也提到了,就是数组是多相的,也就是说元素可以是多种类型的。
And then, when I got- I'm also going to count how many comparisons I do, just so I can see how much work I do here, and then notice what it does.
并能看到算法干了些什么-,然后是如果这个索引,比数组的大小小的话,就意味着还没检索到数组的最后。
All right? If I'm gambling, I'm saying, gee, if I'm really lucky, it'll be only on this side, and I've got a little bit of work to do, but if I'm unlucky, I'm scrawed, the past pluperfect of screwed, OK., or a Boston fish.
如果我够幸运的话,目标数就会在这边的,这会大大减少我的工作量,但如果不幸运呢?我就完蛋了,我就得再从剩下的这么大的数组里找了,这很痛苦,因此选中位数是正确答案。
Remember what range does, it gives me a set or a collection or a tuple of integers from 0 up to 1 - is the last value, so it's going to give me everything from 0 up to the total number of heads.
我将要在这个循环范围内加上鸡的数量,记住循环范围的目的,它给了我一个从一开始的整数集合,或者数组,这是最后的值。
But if, at any point, I get to a place in the list where the thing I'm looking for is smaller than the element in the list, I know everything else in the rest of the list has to be bigger than that, I don't have to bother looking anymore.
比当前位置数组的元素要小,我也就知道后面的数肯定,也都比我的目标数要大了,我就不用再继续进行下去了,这意味着目标数不在这个数组中,我就可以退出了。
If it's there, I'm done, if not, I keep walking down, and I only stop when I get to a place where the element I'm looking for is smaller than the value in the list., in which case I know the rest of this is too big and I can stop.
并且保持遍历,我只在当当前位置的数组元素,大于目标数时停止,这意味着剩下的元素都比目标元素大,但是其他的情况,我还是要遍历完整个数组。
And we're going to start with one that you many of you, have already bumped into perhaps by accident, which are lists.
曾经遇到过的,可能是偶然遇到的类型来开始,就是数组,数组和字符串类型。
Constant. Ooh, constant says, no matter what the length of the list is, I'm going to take the same amount of time.
这个算法用的时间是相同的,我不这么认为,如果我们创建一个十倍于以前大小的数组。
I shouldn't say list, those two tuples, and walk through them to find the pieces that match up.
除数数组进行对比,我不该说数组的,是元组,遍历这两个元组找到相同的元素。
And in that case, in general lists, I need to figure out what's the access time.
一些浮点数,一些字符串,一些数组内部的数组,各种各样。
Which may take up some arbitrary amount of memory. In that case, I'm back to this problem.
然后将接下来的每一个内存块设置为,指向数组对应元素值的指针。
The easy thing to do would be the following: start at the front end of the list, check the first element. If it's the thing I'm looking for, I'm done. It's there. If not, move on to the next element. And keep doing that.
从数组的第一个元素开始:,如果这是目标元素那么结束,如果不是的话就继续比较下面的元素,并一直这么比较下去,但如果,在某一点我发现我要寻找的数字。
The better way to think about this is, suppose, rather than starting at the beginning, I just grabbed some spot at random, like this one.
不从数组的头开始遍历,我随即的从数组中抓一个元素,比如这个,我去看看这个元素的值。
I'll look at the rest of that big chunk of the list, and that's a pain.
因为这样每一步,都可以保证,排除当前数组的。
And the example I want to look at is, suppose I want to search a list that I know is sorted, to see if an element's in the list.
看看目标元素在不在数组里,也就是说我要去,检索一个有序的数组。
Right.? I'm going to set up a variable to say, none what's the answer I want to return, is it there or not. Initially it's got that funny value none.
看我要找的答案在不在数组里面,初始化为,然后建立一个索引。
It says, well I'm going to print out first and last just so you can see it, and then I say, gee 2 if last minus first is less than 2, that is, if there's no more than two elements left in the list, then I can just check those two elements and return the answer.
然后它计算了尾点和开始点的差,如果小于2的话,也就是说数组中的元素小于等于,我对这两个元素进行比较,然后返回结果就可以了,否则的话,我们就去寻找中值点,注意它是怎么实现的,首先这个指向一个列表的开头。
So one of the things I didn't say, it's sort of implicit here, is what is the thing I measuring the size of the problem in?
我有一点没有提及,这有点含蓄,这一点就是我怎么,来度量输入问题的大小呢?,一个数组的大小怎么来定义呢?
应用推荐