为什么 Python 里面的 range 不包含上界?
例如 range(1,5),其实只包含1,2,3,4 即便是range(0,6,2),也不包含6。如果数不在range里那在range里出现这个数字的含义何在? 数组从0开始记数我已经忍了,range不含上界这是一个什么意思?这还让slice等等操作如何被人类理解啊? 另外,左闭右开是哪里来的习惯?有什么渊源? 题主赞同下列答案: “range 的上界参数并非数学意义上的上界,而是计算意义上的跳出循环的条件(当把循环结构作用于这个 range 时,遇到“上界”,就意味着跳出);与 C / C++ 中数组指针为什么被允许指向数组后面的一个元素的原因类似。题主可以查查 Andrew Koenig 在这方面非常细致深入的讨论。” 看见轮子老师的答案我不禁了解了一件事:程序员果然固执。。range 接受的第二个参数又不是数组长度,而是数组的上界。难道你要让我们都认为range(1,6) 中的6是这个数组的长度?1,2,3,4,5 的长度是6?range(5,6) 6 是数组长度? 实际上我赞同range(start,end) 是对应于for(int i=start, i。。不要用C语言来辩解了。我学过C,我知道数组从0开始,但我无法理解range 不含上界。学习下matlab 和Mathematica 以及其他工程软件中看看工程师和科学家怎么想的吧,轮子哥。 另外本题的重点不是数组从0,而是range 的上界。不要抓住dijkstra 这朵奇葩不放谢谢。那个数学家定义的区间是不含右界的请告诉我。我必定谦恭学习
回复内容:
Why are standard iterator ranges [begin, end) instead of [begin, end]? 比较一下下面三种划分实数的方案[0, 1] (1, 2) [2, 3]…
(0, 1) [1, 2] (2, 3)…
[0, 1) [1, 2) [2, 3)…
显然最后一种方案最美,所有区间包含的元素“一样多”。
当然,这里只是说明半开半闭/半闭半开作划分是最合适的,两者相比之下半闭半开更自然。
天天说什么数学家数学家,题主你个学物理的就不要来指导江山教我们应该怎么设计语言啊。尽管你很固执,我还是想开化开化你。
为什么左闭右开,为什么数组从0开始,这是因为C语言获得一个数组a的元素地址&a[b]就可以直接翻译成&a+b*sizeof(*a),免去了一次减法。这次减法在C语言刚发明的时候可是很重要的哦!然后你有一个长度s,那你遍历数组用的for循环就是for(i=0;i
好了,python又不是C语言,但是(据说)python的第一个实现是C写的哦!几乎所有语言受到C的影响全部都做成这样了。当然有少部分的语言不是,譬如说Visual http://Basic.NET,譬如说我为我自己的www.gaclib.net 内置脚本引擎做的傻逼小脚本(最后发现swift的关键设计跟我的完全一致,太丢脸了)。这纯粹看语言他爹的喜好。python他爹就不是什么数学家,哈哈哈哈。
不过我们已经互相拉黑了,我也不用看到题主你的评论了,好开心。
Why are standard iterator ranges [begin, end) instead of [begin, end]?
楼下 @shell von 的一个链接,我发现数学家Dijkstra也是这么想的。
关于这个问题,我想应该是这样:我从c++ 里面迭代器的内容尝试解答一下这个问题,
对于一个容器,我们需要知道它的头尾,最常进行的操作是从begin到end不断+1来循环,此时如何知道循环到达末尾呢,如果end是上界(即包含这一个元素),我们还需对它的下一个元素抛出一个可以判别的信号,那么end的意义就不大了,因此对循环而言,末尾的下一个元素更加重要,可以终止循环。
可以验证这一想法的是,如果采用reversed 迭代器,那么begin指向最后一个元素(包含),而此时的end指向第一个元素的前一个元素,对容器而言,是一个左开右闭的状态,迭代器+1时实际上是向前走。
python中的range不也产生一个list么,遍历总是需要退出的,左闭右开是因为我们总是从list左边开始,遍历到右边结束。
希望有所帮助。 哪个数学家经常用到range
数学家的尊严何在!? 为何楼上都在说数组从0开始这个。我觉得和这个问题毫无关系。
其实程序里使用左闭右开的原因是:
考虑一个区间[l, r], 如果我需要遍历这个区间,需要迭代器支持比较操作。
即:
i = l; while (i <= r) do { process(i); i++; }
——————————————————————————————————————————
python数组
>>> s=[1,2,3,4,5,6]
>>> s
[1, 2, 3, 4, 5, 6]
>>> for i in range(0,6):
... print s[i]
其实来源于
c语言的数组
int arr[6]={0,1,2,3,4,5};//0 to 5...
数组遍历
for(int i=0;i
}
ps:c语言常用i1.i2.极端情况下,如果数组长度为0的话且使用了unsigned int,i——————————————————————————————————————————
再举个例子
2011年到2012年过了多少年,如果算左右界的话:
len(year[2011:2012])=2
这不符合常理
所以左右界中只能算一个
才是
len(year[2011:2012])=1 别犟嘴了,看官方doc不好么?
4.3 The range() Function
说白了,range(a, b)在设计的时候默认a是default as 0的,所以b本来就是当做offset或者说长度存在的一个arg,本来就不是tail index,当你的a不是default的时候,你自然而然以为“应该是一个头一个尾两个index啊?!”,很抱歉,其实不是的。
犟嘴不长学问。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 > /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

This article will explain in detail how PHP returns all the values of an array to form an array. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Using the array_values() function The array_values() function returns an array of all the values in an array. It does not preserve the keys of the original array. $array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values will be ["bar","qux"]Using a loop can Use a loop to manually get all the values of the array and add them to a new

Solution to node start error: 1. Execute "node xx.js" directly in the terminal; 2. Add start startup item "scripts": {"test": "echo \"Error: no test specified\" && exit 1 ","start":"node service.js"}"; 3. Re-execute "npm start".

Preface In Java, the Range method is available in both IntStream and LongStream classes. In IntStream class, it helps to return the sequential value of IntStream within function parameter scope. In the method, startInclusive(inclusive) and endExclusive(exclusive) are the two parameters used along with the increment step size, as mentioned before, the start value will be included and the end value will be excluded. In the case of LongStream, the only difference is the addition of the LongStream value. Range Syntax Let us see the syntax of range method in Java. IntStream range

Go language is a concise and powerful programming language with unique design and features in many aspects. One of the most impressive features is the range keyword, which is used to iterate over data structures such as arrays, slices, maps, and channels. The flexibility and convenience of range make it easy to traverse complex data structures, but many people are confused about how it works. This article will explain how range works in a simple and in-depth way, and use specific code examples to help readers better understand. First, let's look at a simple example

GM's electric take on the legendary Escalade just got its configurator up, and the price ranges from$130,000 to about $170,000 after incentives. This is more than nearly any other luxury electric vehicle, pickup, or SUV out there, including Tesla's C

start method and run method The $start()$ method is used to start a thread. At this time, the thread is in the ready (runnable) state and is not running. Once the $cpu$ time slice is obtained, the $run()$ method starts to be executed. . Directly calling the $run()$ method only calls a method in a class, which is essentially executed in the current thread. Therefore, it can only be achieved by using the $start()$ method to call the $run()$ method. True multithreading. Sample code@Slf4j(topic="c.Test4")publicclassTest4{publicstaticvoidmain(Strin

1. What is the range() function? The range() function is Python's built-in function. It returns a series of consecutively added integers and can generate a list object. Most of them often appear in for loops and can be used as indexes in for loops. Small question practice: for..range Exercise 1: Use for loop and range to find all even numbers between 0 and 100, and append them to a list. list1=[]foriinrange(0,100,2):list1.append(i)print(list1)2: Use for loop and range to find the numbers between 0 and 50 that are divisible by 3, and append them to a list. list2
