4

So first of all, I need to extract the numbers from a range of 455,111,451, to 455,112,000. I could do this manually, there's only 50 numbers I need, but that's not the point.

I tried to:

for a in range(49999951,50000000):
print +str(a)

What should I do?

FZEROX
  • 81
  • 1
  • 1
  • 5

4 Answers4

14

Use sum

>>> sum(range(49999951,50000000))
2449998775L

It is a builtin function, Which means you don't need to import anything or do anything special to use it. you should always consult the documentation, or the tutorials before you come asking here, in case it already exists - also, StackOverflow has a search function that could have helped you find an answer to your problem as well.


The sum function in this case, takes a list of integers, and incrementally adds them to eachother, in a similar fashion as below:

>>> total = 0
>>> for i in range(49999951,50000000):
    total += i

>>> total
2449998775L

Also - similar to Reduce:

>>> reduce(lambda x,y: x+y, range(49999951,50000000))
2449998775L
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • I really appreciate your answer.I had searched around before asking but I guess I'm a heck of a newbie. I also searched again because I wanted to avoid the L(long) at the end.Im using `sum(range(49999951,50000000))\ print math.factorial(100)` Sorry for bothering you. – FZEROX Jul 04 '13 at 14:40
  • @FZEROX You can not avoid the `L` (long) at the end, because the result of the sum of your numbers is simply larger than the maximum of an integer, so it must be a long. I am not sure what you mean by "Im using `sum(range(49999951,50000000))\ print math.factorial(100)`" If this is a follow-up question, please ask a new question. – Inbar Rose Jul 04 '13 at 14:41
  • This was what came up after searching on how to avoid the L, I may be completely off, I just started python and programming. – FZEROX Jul 04 '13 at 14:43
  • 1
    Well - the `L` only apears as the `repr` of the value of the result... if you `print` the value, it will not have the `L` at the end, the `L` is just there to let *you* know that it is a long. – Inbar Rose Jul 04 '13 at 14:53
5

sum is the obvious way, but if you had a massive range and to compute the sum by incrementing each number each time could take a while, then you can do this mathematically instead (as shown in sum_range):

start = 49999951
end = 50000000

total = sum(range(start, end))

def sum_range(start, end):
    return (end * (end + 1) / 2) - (start - 1) * start / 2

print total
print sum_range(start, end)

Outputs:

2449998775
2499998775
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • In my opinion, this is better. Although not more pythonic, this is more efficient. – John Doe Jul 04 '13 at 14:41
  • 1
    @JohnDoe It all depends on what you are looking for - the `sum_range` method that is given here is a much faster, more efficient way of getting the sum, but it is only for a specific type of calculation - the sum total of all integer numbers between X, and Y. There is no option here for skipping numbers, or multiple ranges, or multiple types - only the sum of integers between X and Y... :) so depending on the needs, sometimes the more direct approach is best, other times, the more robust, generic, and portable option is best. – Inbar Rose Jul 04 '13 at 14:45
  • 1
    @InbarRose A slight tweak in the maths and it can do that, but I completely agree using `sum` is the most obvious and easily adapted solution. – Jon Clements Jul 04 '13 at 14:47
  • 2
    Let me rephrase, "this is the better solution to **this** question". – John Doe Jul 04 '13 at 14:49
1

I haven't got your question well if you want to have the sum of numbers

sum = 0
for a in range(x,y):
    sum += a
print sum

if you want to have numbers in a list:

lst = []
for a in range(x,y):
    lst.append(a)
print lst
Moh Zah
  • 262
  • 1
  • 2
  • 9
  • A. Don't shadow the built-in `sum` function. B. `range(x,y)` already yields a list, no need to iterate over it and assign each value to a new list... – Inbar Rose Jul 04 '13 at 14:37
  • If you notice one who ask a question like this is a python beginner definitely. so A. somebody has already mentioned using sum so I just gave the questioning guy another alternative. B. range doesn't yield a list in python 3 if I remember right – Moh Zah Jul 04 '13 at 15:06
  • A. that is still no excuse to shadow the builtin `sum` (shadowing means using the same name as...) B. This question is not marked as either Python 2 or 3.. and even if so, that is the least efficient way to convert an iterable to a list, the best way is simply: `list(range(x,y))` (if indeed it is Python 3 and `range()` returns an iterable and not a list) – Inbar Rose Jul 04 '13 at 15:08
  • excuse? :) well probably this example may clarify my point. There is a classical question which ask to write a piece of code that adds two big numbers e.g. 99999999999999 which are stored in a string. depending on what language you use you can simply use a library and add two big numbers but there is still a learning point in solving the problem by some low level code you write yourself. – Moh Zah Jul 04 '13 at 17:19
-1

try this!!

import re

fhand = 'Actual_regex_sum_1232793.txt'
fopen = open(fhand)
nlist = list()
for lines in fopen:
line = lines.rstrip()
stuff = re.findall('([0-9]+)', line)

if len(stuff) < 1:
    continue

for i in range(len(stuff)):
    num = int(stuff[i])
    nlist.append(num)
print(sum(nlist))
Flair
  • 2,609
  • 1
  • 29
  • 41
Mahesh Kumar
  • 7
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '22 at 12:29