range

Unbounded range()

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 22:43:40
Is there an unbounded version of range (or xrange for Python 2), or is it necessary to define it manually? For example squares = (x*x for x in range(n)) can only give me a generator for the squares up to (n-1)**2 , and I can't see any obvious way to call range(infinity) so that it just keeps on truckin'. You're describing the basic use of itertools.count : import itertools squares = (x*x for x in itertools.count()) 来源: https://stackoverflow.com/questions/7186336/unbounded-range

Multiplication of two integers in C++

自古美人都是妖i 提交于 2019-11-26 22:26:15
问题 I have a pretty basic question, but I am not sure if I understand the concept or not. Suppose we have: int a = 1000000; int b = 1000000; long long c = a * b; When I run this, c shows negative value, so I changed also a and b to long long and then everything was fine. So why do I have to change a and b , when their values are in range of int and their product is assigned to c (which is long long )? I am using C/C++ 回答1: The int s are not promoted to long long before multiplication, they remain

Python 3 range Vs Python 2 range

时间秒杀一切 提交于 2019-11-26 22:23:56
问题 I recently started learning python 3. In python 2 range() function can be used to assign list elements. >>> A = [] >>> A = range(0,6) >>> print A [0, 1, 2, 3, 4, 5] where as in python 3 when range() function is used this is happening >>> A = [] >>> A = range(0,6) >>> print(A) range(0, 6) why is this happening? why did python do this change? Is it a boon or a bane ? 回答1: Python 3 uses iterators for a lot of things where python 2 used lists .The docs give a detailed explanation including the

converting a list of integers into range in python

痴心易碎 提交于 2019-11-26 22:13:57
Is there something existing in python that can convert an increasing list of integers into a range list E.g. given the set {0, 1, 2, 3, 4, 7, 8, 9, 11} I want to get { {0,4}, {7,9}, {11,11} }. I can write a program to do this, but want to know if there is an inbuilt function in python Using itertools.groupby() produces a concise but tricky implementation: import itertools def ranges(i): for a, b in itertools.groupby(enumerate(i), lambda (x, y): y - x): b = list(b) yield b[0][1], b[-1][1] print list(ranges([0, 1, 2, 3, 4, 7, 8, 9, 11])) Output: [(0, 4), (7, 9), (11, 11)] You can use a list

Dynamic chart range using INDIRECT: That function is not valid (despite range highlighted)

邮差的信 提交于 2019-11-26 22:11:02
I'm trying to create a chart with a range built dynamically using the INDIRECT function. Excel does recognize the range I am creating using INDIRECT as it highlights the corresponding range on the sheet: However when saving the chart, I get an error message saying the function is not valid: Does anybody know what the problem is / how to create a dynamic chart range from a specific start to specific end point? PS: You can download the above spreadsheet here . The formula I was using: =INDIRECT("sheet!"&E2&":"&E3) Mine is similar to Sean's excellent answer, but allows a start and end day. First

How to check if an integer is within a range?

北战南征 提交于 2019-11-26 22:07:28
Is there a way to test a range without doing this redundant code: if ($int>$min && $int<$max) ? Like a function: function testRange($int,$min,$max){ return ($min<$int && $int<$max); } usage: if (testRange($int,$min,$max)) ? Does PHP have such built-in function? Or any other way to do it? I don't think you'll get a better way than your function. It is clean, easy to follow and understand, and returns the result of the condition (no return (...) ? true : false mess). Tested your 3 ways with a 1000000-times-loop. t1_test1: ($val >= $min && $val <= $max) : 0.3823 ms t2_test2: (in_array($val, range

Range-based for statement definition redundancy

眉间皱痕 提交于 2019-11-26 21:48:28
问题 Looking at n3092, in §6.5.4 we find the equivalency for a range-based for loop. It then goes on to say what __begin and __end are equal to. It differentiates between arrays and other types, and I find this redundant (aka confusing). It says for arrays types that __begin and __end are what you expect: a pointer to the first and a pointer to one-past the end. Then for other types, __begin and __end are equal to begin(__range) and end(__range) , with ADL. Namespace std is associated, in order to

How to loop backwards in python? [duplicate]

纵然是瞬间 提交于 2019-11-26 21:33:28
This question already has an answer here: Loop backwards using indices in Python? 14 answers I'm talking about doing something like: for(i=n; i>=1; --i) { //do something with i } I can think of some ways to do so in python (creating a list of range(1,n+1) and reverse it, using while and --i , ...) but I wondered if there's a more elegant way to do it. Is there? EDIT: Some suggested I use xrange() instead of range() since range returns a list while xrange returns an iterator. But in Python 3 (which I happen to use) range() returns an iterator and xrange doesn't exist. Chinmay Kanchi range() and

Need to set cursor position to the end of a contentEditable div, issue with selection and range objects

纵饮孤独 提交于 2019-11-26 21:26:36
问题 I'm forgetting about cross-browser compatibility for the moment, I just want this to work. What I'm doing is trying to modify a script (and you probably don't need to know this) located at typegreek.com The basic script is found here. Basically what it does is when you type in characters, it converts the character your are typing into greek characters and prints it onto the screen. What I'm trying to do is to get it to work on contentEditable div's (It only works for Textareas) My issue is

NameError: global name 'xrange' is not defined in Python 3

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 21:20:09
I am getting an error when running a python program: Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in <module> File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in __init__ File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\class\inventory.py", line 17, in __init__ builtins.NameError: global name 'xrange' is not defined The game is from here . What causes this error? You are trying to run a Python 2 codebase with Python 3. xrange() was renamed to range() in Python 3.