756 Answered Questions for the topic Python
06/06/19
Difference between __str__ and __repr__?
What is the difference between `__str__` and `__repr__` in `Python`?
How do I install pip on macOS or OS X?
I spent most of the day yesterday searching for a clear answer for installing `pip` (package manager for Python). I can't find a good solution.How do I install it?
Python Iteration
06/06/19
How to remove items from a list while iterating?
I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. for tup in somelist: if determine(tup): code_to_remove_tupWhat...
more
Decoding Simulink's UDP Packets in Python?
I have a Simulink model sending data via UDP to another program (Blender) where I can receive the packets, but I have not been able to figure out how to correctly decode them.
In the Simulink...
more
Create a python script to simulate a Matlab file and store results?
I have recently started working as a student assistant and I have to write a script in Pycharm (python) which is something new for me.
My task is to write a python script which takes a Matlab file...
more
06/03/19
Least Astonishment and the Mutable Default Argument?
Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return aPython novices would expect this function to...
more
Python Dictionary
06/01/19
Should I use 'has_key()' or 'in' on Python dicts?
I wonder what is better to do: d = {'a': 1, 'b': 2} 'a' in d Trueor: d = {'a': 1, 'b': 2} d.has_key('a') True
05/29/19
Get delta values of each quarter from cumulated income statement reports with pandas?
Get value of each quarter from cumulated income statement reports with pandas
Is there any way to do it with python/pandas?
I have an example dataset like below.
(please suppose that this...
more
Python Geometry
05/29/19
Good geometry library in python?
I am looking for a good and well developed library for geometrical manipulations and evaluations in python, like:
- evaluate the intersection between two lines in 2D and 3D (if present)
-...
more
05/29/19
How do I check if cartesian coordinates make up a rectangle efficiently?
The situation is as follows:
- There are N arrays.
- In each array (0..N-1) there are (x,y) tuples (cartesian coordinates) stored
- The length of each array can be different
I want to extract the...
more
Find equidistant points between two coordinates?
I have a function which needs to take out equidistant points between two points on the screen(2d).
Like this -
|--------------|
The distance is already decided. For example, I take it as `2`...
more
Python Geometry
05/29/19
Estimating aspect ratio of a convex hull?
What would be the best way to approximate the aspect ratio of a convex hull in Python? I have already tried doing this by fitting the vertices of the convex hull with an ellipse and taking the...
more
Finding cycle of 3 nodes ( or triangles) in a graph?
I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative...
more
05/29/19
Finding all points common to two circles?
In Python, how would one find all integer points common to two circles?
For example, imagine a Venn diagram-like intersection of two (equally sized) circles, with center-points `(x1,y1)` and...
more
How can you determine a point is between two other points on a line segment?
Let's say you have a two dimensional plane with 2 points (called a and b) on it represented by an x integer and a y integer for each point.
How can you determine if another point c is on the line...
more
How can I get more exact decimal values in Python?
from math import sqrt
a=1e-8
b=10
c=1e-8
x1 = ((-b)-sqrt((b**2)-(4*a*c)))/(2*a)
x2 = ((-b)+sqrt((b**2)-(4*a*c)))/(2*a)
print 'x1 = {}'.format(x1)
print 'x2 = {}'.format(x2)
...
more
05/29/19
django test app error - Got an error creating the test database: permission denied to create database?
When I try to test any app with command (I noticed it when I tried to deploy myproject using fabric, which uses this command):
python manage.py test appname
I get this error:
Creating test...
more
Renaming columns in pandas?
I have a DataFrame using pandas and column labels that I need to edit to replace the original column labels. I'd like to change the column names in a DataFrame `A` where the original column names...
more
Python File Io
05/29/19
Correct way to write line to file?
I'm used to doing `print >>f, "hi there"`However, it seems that `print >>` is getting deprecated. What is the recommended way to do the line above?Update:Regarding all those answers...
more
05/28/19
mysql_config not found when installing mysqldb python interface?
I am trying to get a Python script to run on the linux server I'm connected to via ssh. The script uses mysqldb. I have all the other components I need, but when I try to install mySQLdb via...
more
05/28/19
Display number with leading zeros?
Given: a = 1 b = 10 c = 100How do I display a leading zero for all numbers with less than two digits?That is, 01 10 100
05/28/19
How to get line count cheaply in Python?
I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most efficient way both memory- and time-wise?At the moment I do: def file_len(fname): ...
more
05/28/19
How do I parse a string to a float or int in Python?
In Python, how can I parse a numeric string like `"545.2222"` to its corresponding float value, `545.2222`? Or parse the string `"31"` to an integer, `31`?I just want to know how to parse a *float*...
more
05/27/19
How do I sort a list of dictionaries by a value of the dictionary?
I have a list of dictionaries and want each item to be sorted by a specific property values.Take into consideration the array below, [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]When...
more
Use of *args and **kwargs?
So I have difficulty with the concept of `*args` and `**kwargs`.So far I have learned that: * `*args` = list of arguments - as positional arguments * `**kwargs` = dictionary - whose keys become...
more
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.