Tagged Questions
28
votes
15answers
10k views
Generating non-repeating random numbers in Python
Ok this is one of those trickier than it sounds questions so I'm turning to stack overflow because I can't think of a good answer. Here is what I want: I need Python to generate a simple a list of ...
23
votes
5answers
11k views
How to limit the maximum value of a numeric field in a Django model?
Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall ...
17
votes
3answers
26k views
Python: Extract numbers of a string
I would extract all the numbers contained in a string. Which is the better suited for the purpose, regular expressions or the isdigit() method?
Example:
line = "hello 12 hi 89"
Result:
[12, 89]
13
votes
5answers
14k views
How to convert a negative number to positive?
How can I convert a negative number to positive in Python? (And keep that positive value.)
11
votes
5answers
2k views
Python: most pythonic way to check if an object is a number
Given an arbitrary python object, what's the best way to determine whether it is a number? Here is is defined as acts like a number in certain circumstances.
For example, say you are writing a vector ...
11
votes
7answers
7k views
Is there a way to Convert Number words to Integers? Python
I need to convert 'one' into '1' 'two' into '2' and so on.
Is there a way to do this a library or a class or anything?
10
votes
5answers
10k views
How do we remove all non-numeric characters from a string in Python?
Quite a simple question, but I'm a python noobie. How do we remove all non-numeric characters from a string in Python?
How can I do that?
10
votes
2answers
516 views
Dividing decimals yields invalid results in Python 2.5 to 2.7
After a very thorough read of the Python's decimal module documentation, I still find myself puzzled by what happens when I divide a decimal.
In Python 2.4.6 (makes sense):
>>> import ...
9
votes
3answers
1k views
How to convert strings numbers to integers in a list?
I have a list say:
['batting average', '306', 'ERA', '1710']
How can I convert the intended numbers without touching the strings?
Thank you for the help.
9
votes
8answers
5k views
range and xrange for 13-digit numbers in Python?
range() and xrange() work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.
9
votes
2answers
2k views
How can I represent an infinite number in Python?
For example when you want in python to give to a set of elements an associated value, and you use this value for comparisons between them, I would want this value is infinite, I mean, no matter which ...
8
votes
6answers
812 views
Irrational number representation in any programming language?
Does anyone know of an irrational number representation type/object/class/whatever in any programming language?
All suggestions welcome.
Simply put, if I have two irrational objects, both ...
8
votes
4answers
808 views
Python augmented assignment issue
i ran into something interesting about the python augmented assignment +=
it seems to be automatic data type conversion is not always done for a += b if a is a 'simpler' data type, while a = a + b ...
8
votes
4answers
195 views
Python - Check if the last characters in a string are numbers
Basically I want to know how I would do this.
Here's an example string:
string = "hello123"
I would like to know how I would check if the string ends in a number, then print the number the string ...
8
votes
6answers
490 views
Generate all possible numpad/keypad sequences
I am trying to generate all possible keypad sequences (7 digit length only right now). For example if the mobile keypad looks like this:
1 2 3
4 5 6
7 8 9
0
Some of the possible sequences can be:
...