I have a large variable in Python. To be more exact, it's 1000 numbers long.
What I'd like to do is to get every number in... not sure how to explain, a batch of five?
an example to hopefully clearify, I have a long like this
1415926535897932
And I'd like to get 14159 in a temporary variable, and then 24592, and then 45926, etc.
My first idea was to do this using slicing
[code]
for i in range(1000):
if Digits[a:b] > greatest:
#Do stuff here (and increase a and b of course)
[/code]
However, since Digits is classified as long, I get this error:
[code]
TypeError: 'long' object is unsubscriptable
[/code]
I know I should probably use another way then, especially since this is something which will be done 1000 times. What would be the best way to achieve this?
(This is for a problem at [url=http://projecteuler.net/index.php?section=problems]Project Euler[/url], so... please don't give the full solution away, just saying "read up on [subject]" would be great >_<)
Convert the long to a string, then slice the string. Convert the slice back to a number.
How about that?
You'll have to convert it to a string first. Then something like this, which has less iterations. (Pseudo code, I don't know Python)
[code]
while i < len(str)/5:
cut = str.substr(i*5, (i+1)*5)
i++
[/code]
(digits//(100000**i))%100000 is another option if you want to avoid converting between types.
jA_cOp, I tried that just with an integer, but that didn't work, I'll try out your way. Thanks.
Vampired, not totally sure I get what that is doing. if I is less then the lenght of the string devided on five, cut the string at i*5 (fine enough, to get the starting point) and end the cut at.. ahh, of course. But that would fail at the first character (lenght starts at 0, though that would be easy to get away with). Thanks, gave me a better idea at reading code >_<
Robo, your idea is basicly to divide it down into a decimal.. or? Thanks anyway though. >_<
Got a plane to catch, so I'll not be on for... 13 hours or so. :/
Thanks for all of your inputs, will give me something to tinker with.
I was just trying to shorten the number of iterations but probably made thing more confusing. I'm sure your function would do the job if you converted it to a string first.
[QUOTE=Warsheep;16351811]
Robo, your idea is basicly to divide it down into a decimal.. or? Thanks anyway though. >_<[/QUOTE]
The "//" operator means "floor division". It's like integer division with the remainder discarded. By dividing by powers of 100000, we can shift the number five decimal places for every increment of i. "%" is the modulus operator. It's like the remainder in integer division. Taking the modulus of the result and 100000 gives us the last five digits of the result.
So we shift the number to the right as many times as we want, then take the last five digits.
[QUOTE=Warsheep;16351013]I have a large variable in Python. To be more exact, it's 1000 numbers long.
What I'd like to do is to get every number in... not sure how to explain, a batch of five?
an example to hopefully clearify, I have a long like this
1415926535897932)[/QUOTE]
Much easier to convert to string and back, you'll be dealing with MUCH larger numbers in later problems ;)
Ahhh, now I get it, smart...
Though, I'll probably try the convertion from string and back, as that's something I think I already halfway understand.
While the way Robo_Donut might teach me a lot more, it'll be much more difficult and probably ten times more difficult.
About the string conversion, how will that be done?
testvariable = str.oldlongvariable?
Or ..
testvariable = str.oldlongvariable[0:4] ?
Sorry for a lot of noobish questions, but I have no where else to ask >__<
I think to convert a number to a string in Python you do:
foo = str(number)
Sorry, you need to Log In to post a reply to this thread.