我认为计算机科学学习中最重要的概念之一是理解数组索引的工作原理。你应该花点时间用简单的例子向学生解释这个概念。从Lisp开始,几乎
全部的
编程语言以
Zero-based numbering
. Python和C++没有什么不同。在python中,简单
lists
可以作为例子来说明这个概念。
#a simple list
In [15]: list1 = [1,2,3,4,5,6,7,8,9,10,11]
In [23]: len(list1) #number of items in the list
Out[23]: 11
In [26]: list1[0] #the first item in the list
Out[26]: 1
In [25]: print("list1 starts at index {}. The number at this index is {}.".format(list1.index(1), list1[0]))
list1 starts at index 0. The number at this index is 1.
In [37]: list1[10] #the last item in the list
Out[37]: 11
In [19]: print("list1 ends at index {}. The number at this index is {}.".format(len(list1)-1, list1[-1]))
list1 ends at index 10. The number at this index is 11.
正如您将观察到的,“number”值在“index”值之前
1
. 如果名单上
list1
以0开始,以11结束,则索引值和“数字”值将相同,但列表中的项目数将增加
一
这是因为我们包含了0。我们需要这样的想法,请稍等片刻:
In [29]: list2 = [0,1,2,3,4,5,6,7,8,9,10,11]
In [31]: len(list2) #total number of items in list
Out[31]: 12
In [32]: list2[0]
Out[32]: 0
In [35]: list2[11]
Out[35]: 11
记住
range()
根据文件:
range(start, end, step)
. 我们会忽略的
步
为了这次讨论。
所以,现在如果我想生成一个类似于
list2
使用
距离()
,我可以使用上面的信息来形成range()的通用语法:
Syntax: range(start, len(list2)+start), where start is the first item in the range,
list2 is a list and len(list2) is its size and the end is len(list2) + start
为了将其放入列表中,我只需将上面的作为参数传递给
list()
功能。了解这个函数的本质并不重要,它只用于说明。因此,我们得到:
In [39]: list3 = list(range(0,len(list2)+0))
In [40]: list3
Out[40]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
In [41]: list2 == list3 #both lists are equivalent
Out[41]: True
In [42]: len(list3)
Out[42]: 12 #same number of items as list2
In [46]: for i in range(0,len(list3)+0): #can replace list3 with list2 to get same results
...: print(i, end=' ')
...:
0 1 2 3 4 5 6 7 8 9 10 11 #Output
for循环按索引位置迭代。所以,
i
从项目0的索引位置0(范围开始)开始,一直到项目11的索引位置11(范围结束=11+0)。
我们还可以验证上述语法
表1
那有
开始
长度为11。
In [6]: list(range(1,len(list1)+1))
Out[6]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] #same as list1 above
In [15]: for i in range(1,len(list1)+1):
...: print(i, end=' ')
...:
1 2 3 4 5 6 7 8 9 10 11
同样,for循环像往常一样从索引位置0开始,该位置有项1(范围的开始),到索引位置10结束,该位置有项11(范围的结束=11+1)。
现在,让我们来看看这个概念如何也适用于C++。我们用同样的方法
表2
有12个项目,即
len(list2) = 12
. 同样,如果学生不太熟悉数组,那么在下面的示例中深入了解所有内容的细节并不重要。这只是为了说明:
#include <iostream>
using namespace std;
int main()
{
int list2[12] = {0,1,2,3,4,5,6,7,8,9,10,11};
std::cout << "Length of array list2 = " << (sizeof(list2)/sizeof(*list2)) << std::endl;
for(int i=0;i<(sizeof(list2)/sizeof(*list2));i++)
{
cout<<i<<' ';
}
return 0;
}
//Output:
Length of array list2 = 12
0 1 2 3 4 5 6 7 8 9 10 11 #Output
通知
i<(sizeof(list2)/sizeof(*list2))
确保从数组的上界检索最终项,即从index=12-1=11检索项11。这正是
距离()
函数可以。如果我这样做
i<=(sizeof(list2)/sizeof(*list2))
,也会在不是
表2
将超出数组的上限。在python中,
距离()
函数对数组边界更加明确。所以,如果我重写
距离()
上面的语法并允许for循环从0迭代到后面的项
表2
通过递增
结束
通过1,我可以显式地打印该项:
In [13]: for i in range(0,len(list2)+1):
...: if i>=len(list2):
...: print("\nI am outside of list2: {}".format(i))
...: else:
...: print(i, end=' ')
...:
0 1 2 3 4 5 6 7 8 9 10 11
I am outside of list2: 12
我们从这次讨论中了解到,将循环迭代理解为基于索引的操作和
range(start,end)
是一个python内置函数,它只在开始和结束之间组合值
范围
,
包含起始值,不包含结束值
. 考虑遍历的值的总数,包括
开始
(
len(total) + start
)
结束
范围的。for循环实现与此无关,只考虑python和c++中的索引位置。