Tagged Questions
0
votes
3answers
36 views
Determining if a Variable is Numerically an Integer in Python
I'm having an issue with determining if a variable is an integer. Not the variable type, but the actual value stored in the variable. I have tried using variable % 1 as a test, but it does not seem to ...
0
votes
3answers
25 views
Looping with a while statement while reading a string for an integer
I need help encasing the:
if any(c.isdigit() for c in name):
print("Not a valid name!")
inside of a while statement. This pretty much just reads the input for the "name" variable and sees if ...
1
vote
1answer
38 views
How to convert signed 32-bit int to unsigned 32-bit int in python?
This is what I have, currently. Is there any nicer way to do this?
import struct
def int32_to_uint32(i):
return struct.unpack_from("I", struct.pack("i", i))[0]
4
votes
3answers
88 views
Data structure options for efficiently storing sets of integer pairs on disk?
I have a bunch of code that deals with document clustering. One step involves calculating the similarity (for some unimportant definition of "similar") of every document to every other document in a ...
3
votes
3answers
60 views
Making sure no integers in a string?
I have a simple question. I'm just wanting to know how I would have my program read the "input()" and see if there are integers or any kind of number in the string, and if so then print out a message ...
0
votes
2answers
66 views
String to Int conversion in python
If I have a string "2+3", is there anyway to convert it to an integer so it comes out as 5?
I tried this:
string = 2+3
answer = int(string)
But I get an error:
ValueError: invalid literal for ...
0
votes
1answer
61 views
Python: Read binary file into buffer as integer then Slice
In python, how do i load a binary file into a buffer then read individual bytes as numbers not strings.?
0
votes
1answer
43 views
Why Pythons eval() mistakes?
I need to solve some mathematical equations, something like below (but each time different formula):
formula="(2/10^8*x^2)+0.0131*x-1017.3-30"
where x is an integer.
I used eval() function to ...
-3
votes
4answers
56 views
Python Function that returns greatest integer [closed]
Hi I am trying to create a python function that when I enter in 3 integers, and it has to be 3!, it will return the greatest integer I put in.
so like if I input (3, 4, 5)
it will return to me "5"
...
1
vote
1answer
32 views
Python evaluates comparison of integer and character with the same value as False
I ran into this interesting little 'gotcha' this evening, consider the following code snippet:
( chr(1) == 1 )
This comparison evaluates to False on Python 2.7.4, is this a feature or a bug?, If a ...
0
votes
1answer
47 views
Python — how to get integers found within dictionary
I'm teaching myself Python. I didn't see this covered in the dictionary section of http://docs.python.org/2/tutorial/datastructures.html
I want to convert a string to an integer and back. ...
1
vote
3answers
51 views
Testing for non-integers (decimals and strings)
Hi I'm having trouble testing for non-integers in my sript. I'm trying to write a function that returns True if a sudoku is valid and False if not. 2 rule for Sudoku: 1) Each column of the square ...
-2
votes
3answers
47 views
Returning different types in Python [closed]
Noob question, but:
Say you have a, which equals 90; and b, which equals 100; and you want to return
"90 is less than 100"
How do you get it to return (not print) "90 is less than 100" rather than "a ...
-4
votes
1answer
37 views
Get value from a text and convert it to integer in Python
Imagine I have a text similar to this one:
tplh= 7.0336E-10 targ= 1.0915E-08 trig= 1.0212E-08
tphl= 6.1239E-10 targ= 2.0710E-08 trig= 2.0097E-08
tp= 6.5788E-10
tplhc= ...
0
votes
1answer
65 views
Adding a 'month' 'year' or 'days' to integer field - OpenErp
I'd like to add in an 'integer' field a type of date.
Like = Lifetime of a Product = 6 months
I know i can use integer for the number, like
'Lifetime' : fields.integer('Lifetime'),
But is there a ...
1
vote
3answers
60 views
counting an unspecified number of integers separated by spaces and finding occurrences of each number in python using dictionary
Sample Output
Enter numbers separated by spaces :1 2 3 3 2 2 2 1 3 4 5 3
{'1': 2, '3': 4, '2': 4, '5': 1, '4': 1}
1 occurs 2 times
3 occurs 4 times
2 occurs 4 times
5 occurs one time
4 occurs ...
0
votes
1answer
49 views
Generate and Enter Value for OEIS Sequence in Python?
This is a rather difficult challenge for me as I am new to Python. How would I write a program in python based off this sequence function:
http://oeis.org/A063655
and does the following:
It asks ...
-2
votes
3answers
153 views
Python's isdigit() method returns False for negative numbers
I have a text files that I read to a list. This list contains integers and strings.
For example, my list could look like this:
["name", "test", "1", "3", "-3", "name" ...]
Now, I want to convert ...
1
vote
2answers
106 views
Condition if string contains only variable and integer Python
So, I'm working on a script in Python 3, and I need something like this
control_input=input(prompt_029)
if only string_029 and int in control_input:
#do something
else:
#do something else
...
0
votes
5answers
237 views
Convert strings to int or float in python 3?
integer = input("Number: ")
rslt = int(integer)+2
print('2 + ' + integer + ' = ' + rslt)
double = input("Point Number: ")
print('2.5 + ' +double+' = ' +(float(double)+2.5))
Gives me
Traceback ...
2
votes
6answers
536 views
Integer square root in python
Is there an integer square root somewhere in python, or in standard libraries? I want it to be exact (i.e. return an integer), and bark if there's no solution.
At the moment I rolled my own naive ...
2
votes
1answer
85 views
Python int float rounding
While playing with python, I came across this:
a = 1/(2.2 - 2)
print a #prints out 5.0
print int(a) #prints out 4
I suspect the problem has something to do with binary number representation ...
1
vote
3answers
111 views
How to convert integer to its English representation in Python? [duplicate]
I need to convert integers to their English (string) representation in python.
Example:
5 = five
20 = twenty
etc.
I know, I can use conditions, but I need to convert many integers, so I need the ...
0
votes
2answers
136 views
C# send integer to python
I am trying to send two integers to a python client script (which is running on a Linux machine) from a C# program (the server) which is running on a pc, using socket technology. Until now I am able ...
3
votes
4answers
94 views
Python: split list of integers based on step between them
I have the following problem. Having a list of integers, I want to split it, into a list of lists, whenever the step between two elements of the original input list is not 1.
For example: input = [0, ...
0
votes
1answer
102 views
How to convert raw input number to a list of integers in python?
I have this code written:
guess = list()
playerNumber = raw_input
('Please enter a four digit number for which all the digits are different.')
p = str(playerNumber)
guess = ...
-2
votes
3answers
82 views
Usage of %d does not output an integer [closed]
I am finding it difficult to understand the %dformat specifier. From the documentation, I know that it is a placeholder for integers but it doesn't seem to behave like that when I am using it:
...
0
votes
2answers
183 views
Beginner at Python, math functions for a simple calculator program
I'm making a basic calculator for a programming course and I've read over the PDF's but I can't figure out how to make a function and then use it to print the results of the addition of two numbers. ...
5
votes
3answers
161 views
How to subtract one from every value in a tuple in Python?
I have a tuple created with zip() and I need to subtract one from each integer in the tuple. I tried the following but apparently it only works on lists, so how would I adapt this for tuples in ...
0
votes
3answers
86 views
How to turn a tuple into an integer in Python (examples inside)?
I have a list of tuples:
"(1,2,3), (2,3,1)..."
I would like to change this into a list of integers:
"123, 231..."
How might I go about doing this? Thanks in advance.
2
votes
3answers
147 views
Check if input is a string in a loop
I am currently writing a program in Python 3.3.0 which prints out the n first square numbers and finally prints out their sum. The condition is that the user can only compute an integral number of ...
3
votes
1answer
73 views
Assigning integers to strings
I'm working in Python and I have a list of integers: [1, 2, 1, 3, 2, 2, 1, 3].
I want to assign each integer a string as if it were a variable:
['red', 'orange', 'red', 'yellow', 'orange', 'orange', ...
-1
votes
2answers
59 views
plotting integer vs list using python [closed]
I have made a list using a Python program that contains 5000 elements. I want to plot this list against a list of integers which looks like [-2500:2500]. How can I do this?
1
vote
2answers
105 views
Open a csv as a list of integers
I am a newbie trying to teach myself Python. I have a file with a bunch of numbers in it and I want to "import" them into a python list as integers (or at least that's what I think I want to do). I ...
-1
votes
3answers
118 views
Python remove '\n' from list and then convert strings in list to integers
my_list = []
with open(home + "/Documents/stocks/" + filePath , newline='') as f:
array1 = (line.split(',') for line in f.readlines())
last_col = [line.split(',')[-1] for line in f]
for ...
2
votes
2answers
105 views
Using Python to convert integer to binary
I'm trying to convert integer to binary. This is my work.
I don't know how the make a list to show the binary.
num_str = input("Please give me a integer: ")
num_int = int(num_str)
while num_int ...
0
votes
1answer
63 views
Validate that integer is typed
I've looked up several of these questions and can't seem to apply it to my code correctly. I'm definitely new to Python and developed a number guessing game for practice. The last error handling I ...
18
votes
7answers
441 views
Constructing the largest number possible by rearranging a list
Say I have an array of positive whole integers; I'd like to manipulate the order so that the concatenation of the resultant array is the largest number possible. For example [97, 9, 13] results in ...
1
vote
4answers
315 views
String formatting in Python: can I use %s for all types?
When doing string formatting in Python, I noticed that %s transforms also numbers to strings.
>>> a = 1
>>> b = 1.1
>>> c = 'hello'
>>> print 'Integer: %s; Float: ...
1
vote
5answers
93 views
Validating integers in Python strings
We have a large number of strings containing substrings that are possibly integers eg.
mystring = "123 345 456 567 678 789"
and need to verify that:
a. each substring is in fact an integer eg. in ...
2
votes
2answers
153 views
How to get the integer eigenvectors of a Numpy matrix?
I have a Numpy matrix, for example, numpy.matrix([[-1, 2],[1, -2]], dtype='int'). I want to get its integer-valued eigenvectors, if any; for example, numpy.array([[-1], [1]]) for the above matrix. ...
1
vote
3answers
119 views
Python: using range() with “ ”.join()
x = WAIT100MS
subroutines = ["WAIT"+str(range(1,256))+"MS"]
if x in subroutines:
print "success"
else:
print "invalid"
I'm trying to create a piece of code where if WAITXMS is between 1 and ...
0
votes
4answers
191 views
How to determine whether the number is 32-bit or 64-bit integer in Python?
I want to differentiate between 32-bit and 64-bit integers in Python. In C it's very easy as we can declare variables using int_64 and int_32. But in Python how do we differentiate between 32-bit ...
-3
votes
1answer
73 views
Why do I get error: ValueError: invalid literal for int() with base 10: '1 -3 10 6 5 0 3 -5 2'
I am attempting to make a string an integer by saying
file = open(input("Please enter the name of the file you wish to open:" ))
while True:
A = file.readline()
if(A):
...
-10
votes
4answers
157 views
How do I add together integers in a list in python? [closed]
If I had a list like
x = [2, 4, 7, 12, 3]
What function/process would I use to add all of the numbers together?
Is there any way other than using sum
()?
0
votes
1answer
58 views
Python : Split Amount into Thousands and Lakhs
i have an Amount Object which an Integer that i want to split into Lakhs and Thousands. So for Example say the amount = 2,50,000 i want to split this into lakhs = 2,00,000 and thousands = 50,000.
...
2
votes
7answers
9k views
Python: finding lowest integer
I have the following code:
l = ['-1.2', '0.0', '1']
x = 100.0
for i in l:
if i < x:
x = i
print x
The code should find the lowest value in my list (-1.2) but instead when i print ...
8
votes
7answers
12k views
RGB Int to RGB - Python
How can I convert an RGB integer to the corresponding RGB tuple (R,G,B)? Seems simple enough, but I can't find anything on google.
I know that for every RGB (r,g,b) you have the integer n = r256^2 + ...
97
votes
5answers
49k views
how can I force division to be floating point in Python?
I have two integer values a and b, but I need their ratio in floating point. I know that a<b and I want to calculate a/b, so if I use integer division I'll always get 0 with a remainder of a.
How ...
245
votes
12answers
344k views
Parse String to Float or Int
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?
I just want to know how to parse a float string to a float, and ...