Convert list items to numbers

There is a csv file with the below three lines. 8.84,17.22,13.22,3.84 3.99,11.73,19.66,1.27 16.14,18.72,7.43,11.09 I am writing a function which reads lines from a file and appends it to an empty list so that I can use that list for computing mean. B

call a variable of a string

I am working on a python hangman game and have my graphics based on a variable, lives, so when you have 1 lives, the corresponding picture appears. i put my graphics into definitions and want to call them from that. Here's my code so far: if life== 1

Rank of a permutation

so there was a question I wasn't able to solve mainly because of computing power or lack thereof. Was wondering how to code this so that I can actually run it on my computer. The gist of the questions is: Let's say you have a string 'xyz', and you wa

Take the union of two columns, Python + Pandas

I have a df arranged like follows: x y z 0 a jj Nan 1 b ii mm 2 c kk nn 3 d ii NaN 4 e Nan oo 5 f jj mm 6 g Nan nn The desired output is: x y z w 0 a jj Nan a 1 b ii mm a 2 c kk nn c 3 d ii NaN a 4 e Nan oo e 5 f jj mm a 6 g Nan nn c The logic is to

Empty string instead of an unmatched group error

I have this piece of code: for n in (range(1,10)): new = re.sub(r'(regex(group)regex)?regex', r'something'+str(n)+r'\1', old, count=1) It throws the unmatched group error. But if it is unmatched, I want to add empty string there instead of throwing a

django - set of root paths

When I enter localhost:8000 or localhost:8000/store, both views are same as home/index.html. I want these paths to set different view. from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'', 'home.views.inde

Checking the integrity of the file with Python

I have a directory with many big files. They have all been created with this line of code: pickle.dump(variable,, 'wb'), -1) So they are basically compressed, serialized variables. Now, at some point, a crash/interruption (or seve

Applying the if statement for the pandas column

I have a Probability column, some of the values are greater then 1. I am trying to iterate over every row of that column and change values to 0.99 for all > 0.99. Also I need to see if column 1 is equal to column 2. If yes Probability must be 0. And

How to create a traceback object

I want to create a traceback like the one returned by sys.exc_info()[2]. I don't want a list of lines, I want an actual traceback object: <traceback object at 0x7f6575c37e48> How can I do this? My goal is to have it include the current stack minus o

Print a string as a hexadecimal literal python

I have a lot of pre-existing code that treats byte arrays as strings, i.e. In [70]: x = '\x01\x41\x42\x43' Which python always prints as: In [71]: x Out[71]: '\x01ABC' This makes debugging a pain, since the strings I print don't look like the literal

Cython command to force explicit declaration of all variables

In Cython it is known that an undeclared variable type carries more overhead that slows down the whole process. If this unknown variable type is used inside a nested loop like: def test(b, c, m, n, p): ctype double a for i in range(m) for j in range(

Optimize the solution for Project Euler 12 (Python)

I have the following code for Project Euler Problem 12. However, it takes a very long time to execute. Does anyone have any suggestions for speeding it up? n = input("Enter number: ") def genfact(n): t = [] for i in xrange(1, n+1): if n%i == 0:

Transform the HTML table into JSON

I have a huge HTML Table (about 500,000 rows) that I need to transform into a JSON file. The table looks something like this: <table> <tr> <th>Id</th> <th>Timestamp</th> <th>Artist_Name</th> <th>Tweet_

Python extracting the body from the function to the execution

Given a function in Python defined as follows: a = 3 b = 4 c = 5 def add(): d = a+b+c is it possible to get a code object or similar that gives me: a = 3 b = 4 c = 5 d = a+b+c The function object has a code object associated with it; you can exec tha

appropriate way to stop a demon process

I have a Jython script that I run as a daemon. It starts up, logs into a server and then goes into a loop that checks for things to process, processes them, then sleeps for 5 seconds. I have a cron job that checks every 5 minutes to make sure that th

Learn Python the hard way, Exercise 10.4

#Combine %r with double-quote and single-quote escapes and print them out. #Compare %r with %s. Notice how %r prints it the way you'd write it in your file, #but %s prints it the way you'd like to see it? This is what I wrote for the exercise: 1) myb

question about postgresql link variables

I was looking at the question and decided to try using the bind variables. I use sql = 'insert into abc2 (interfield,textfield) values (%s,%s)' a = time.time() for i in range(10000): #just a wrapper around cursor.execute db.executeUpdateCommand(sql,(

How to group tkinter?

I am distributing an app that uses the Python/C API. I have all standard python modules in which is basically an archive of the Lib folder in the python install directory. Here is the problem - most common modules like sys and io work fi

3D Math Library for Python

i'm looking for a 3d math library in python or with python bindings. it needs to handle rotation, translation, perspective projection, everything basically. what im NOT looking for is a library aimed at drawing on the screen, googling for hours only