This should be simple - In python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222 or "31" to an integer, 31?
EDIT: I just ... |
Given is the following example:
class Foo(object):
def __init__(self, value=0):
self.value=value
def __int__(self):
...
|
Is there some way to just get the next token from a file in Python, as for example the Scanner class does in Java?
File file = new File("something");
Scanner in = new ...
|
Here's a piece of code that takes most time in my program, according to timeit statistics. It's a dirty function to convert floats in [-1.0, 1.0] interval into unsigned integer [0, ... |
it gives me this error:
Traceback (most recent call last):
File "C:\Users\Public\SoundLog\Code\Código Python\SoundLog\Plugins\NoisePlugin.py", line 113, in onPaint
dc.DrawLine(valueWI, valueHI, valueWF, valueHF)
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 3177, in DrawLine
...
|
For example:
import random
x = random.randint(1, 6)
y = 2
new = x / y
...
now, lets say x turns out to be 5.
How can I catch if it's an int or a float before ... |
How can I combine 2 ints to a single 32bit IEEE floating point ? (each of the 2 ints represent 16 bit)
And in the opposite direction: How can I transform a ... |
|
Is there any way to check if a long integer is too large to convert to a float in python?
|
I think the maximum integer in python is available by calling sys.maxint, whereas the maximum float or long, what is it?
|
I'm trying to learn Python, (i have 2.5.4) by writing a snake game, but I'm stuck.
Some integers change into floats and keep changing randomly, at least from my perspective :)
The problem ... |
I'm using boost::python::extract<> to convert the items in a boost::python::list to floats. My problem is with int's in python - extract<float> seems to regard int->float as a valid conversion, however I ... |
This is the model in Google App Engine:
class Rep(db.Model):
mAUTHOR = db.UserProperty(auto_current_user=True)
mUNIQUE = db.StringProperty()
mCOUNT = db.IntegerProperty()
mDATE ...
|
I'm making a webapp that does some data processing, so I frequently find myself parsing strings (from an URL or a text file) into Python values.
I use a function that is ... |
I am missing here the technical word but the problem here is either to change int to float or float to int.
def factorize(n):
def isPrime(n):
...
|
I am working on a Python concurrency framework (I know, YACF) and would like to be able to return variables as Futures, but without the user noticing it.
Right now I am ... |
I couldn't find another answer when I wanted this, so I thought I would post my own solution for anyone else and also get corrections if I've done something wrong.
I had ... |
In a for-loop, I'm integrating with respect to time with constant, fractional time step, dt. I only want to save the simulation results for integral (finite ordinal) time points. My solution ... |
I want to pass a function to a function in Python. I know I can do this simply by putting the function name as a parameter, eg:
blah(5, function)
However, I want to ... |
Basically, I'm converting a float to an int, but I don't always have the expected value.
Here's the code I'm executing:
x = 2.51
print("--------- 251.0")
...
|
As part of some other calculations, I noticed that I sometimes apply float() and then int() function to an integer input. Is it safe to assume that:
int(float(x)) == x
if x is ... |
I have a long list with a bunch of lists:
[0, ['0.2000', '0.2000', '3.0000', '0.5000']]
How do I make the '...' floats and keep the integers (0) integers?
I've tried numpy, but it ... |
I'm trying to read a csv file in python, so that I can then find the average of the values in one of the columns using numpy.average.
My script looks like this:
import ...
|
Out of curiosity I ran the following:
>>> int(1e100)
And, the output was:
10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104L
Why? Why does this not look like:
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
Is this a product of the int function, or of the storage of a large ... |
in other words:
Is distinguishing between ints and floats as important as it use to be?
if this is language dependent, I'm interested in python.
EDIT :
I'm also interested in learning more ... |
x=10.5
if x==10.5:
x=int(x)+1
y= .5
ok i have x=10.5 i want to round up ... |
I have a function fac(n) which return n!, and I am comparing it to gamma(n+1)
>>> from math import gamma
>>> gamma(101)-fac(100)
0.0
>>> math.floor(gamma(101))-fac(100)
0.0
>>> long(gamma(101))-fac(100)
-1716052534060312817912314997891197522637580074354635372754659484313875350886868191008427848256820699487696649234627144617147818134104040275968L
gamma(101) = 100! and is an integer.
why are the ... |
Whenever I try to run this code:
#Open file
f = open("i.txt", "r")
line = 1
#Detect start point
...
|
Hi all first time posting in a while sry if i mess something up. Anyway I am using 2d arrays to make a game with pygame and i alter coordinates on this array by smaller than 1 increments. Then i need to reference the coordinates in an If statement. However by this time in the program the coordinates have been altered ... |
Well i wrote this: number = raw_input('Enter a number: ') int(number) float(number) str(number) print "The integer is: ", number print "The float is: ", float print "The String is: ", str but when i run the program it comes out with: clamshell:~> python number3.py Enter a number: 3 The integer is: 3 The float is: The String is: |
|
import math print "Welcome to MathApplication, the application where you can calculate several distances using formulas" def circle_area(r): return math.pi * (r ** 2) print "Do you wish to calculate the area of a circle, giving its radius? y/n" answer1 = raw_input() if answer1 == "y": r = input("What is the radius? ") ... |
response = raw_input(prompt_phrase) response = response.strip(' ') # remove leading/trailing spaces if not response: # do something because it's blank else: try: ... |
if the float result is bigger than 1.0 but not exactly 2.0 then the integer should be 2 and sequentially rounding all the following float numbers not to their integer (by int() parameter) but by their integer plus 1 if they do not meet the condition of being their own integer like 1.0 = 1 and 2.0 = 2 etc ... ... |
i have a string that looks like: a = ['5,084,900', '3,153,700', '1,086,000'] and id like to do sum them up with something like sum(a[0:3]) but i get an error like this: TypeError: unsupported operand type(s) for +: 'int' and 'str' So i tried to make "a" into floats by doing somethign like... b = floatnums = [float(i) for i in a] ... |
f, i = 0.0, 0 try: line = raw_input("Enter float & int:") list = line.split() if len(list) < 2: print "Bad input" else: f, i = float(list[0]), int(list[1]) except ValueError: print "Bad Input" # safely use f & i |
class Int( int): def __init__( self, pNumb): # int.__init__( self, pNumb << 10) self = ( pNumb << 10) def __str__( self): return "%2.3f" % ( self / 1024) |