Hi,
Fastest way to uniqify a list in Python without preserving order? I saw many complicated solutions on Internet - could they be faster then simply:
list(set([a,b,c,a]))
?
Suppose user enter this string at terminal
123 456 456 //then hit enter
How do I scan these three (could be more) numbers in different variables in python
Could be something like this:
for i in range(1,n)
m[i]=#WHAT FUNCTION SHOULD I PUT HERE
In c++ we can easily use cin>>m[i] inside above loop to scan the variables.
If i use input() or raw_input() , they would scan whole line in single variable.
I have written a simple python module, it has this code:
_log = logging.getLogger("mymodule")
_started = False
def set_log_level(level):
_log.setLevel(level)
if not _started:
_hdlr = logging.FileHandler('mymodule.log')
When I call set_log_level() program fails because symbol _started is not found. It is normal because global _started is missing in the method. But my question is: symbol _log has the same visibility as _started, so why does this symbol can be found?
BR,
// Toby
What's the best way of getting the last item from an iterator in Python 2.6? For example, say
my_iter = iter(range(5))
What is the shortest-code / cleanest way of getting 4 from my_iter?
I could do this, but it doesn't seem very efficient:
[x for x in my_iter][-1]
What are the differences between these built-in Python data types: list, sequence and slice? As I see it, all three essentially represent what C++ and Java call array.
Does anyone know of a memory efficient way to generate very large xml files (e.g. 100-500 MiB) in Python?
I've been utilizing lxml, but memory usage is through the roof.
I'm trying to write a function named median that takes a list as an input and returns the median value of the list.
I'm working with Python 2.7.2
The list can be of any size and the numbers are not guaranteed to be in any particular order.
If the list contains an even number of elements, the function should return the average of the middle two.
This is the code I'm starting with:
def median(list):
print(median([7,12,3,1,6,9]))
I write a python class which makes asynchronous method calls using D-Bus. When my reply_handler is called, it stores data in list. This list can be used by another class methods at the same time. Is it safe or I can use only synchronized data structures like Queue class?
I am trying to get the response codes from Mechanize in python. While I am able to get a 200 status code anything else isn't returned (404 throws and exception and 30x is ignored). Is there a way to get the original status code?
Thanks
Format is like:
CHINA;2002-06-25 00:00:00.000;5,60
CHINA;2002-06-26 00:00:00.000;5,32
CHINA;2002-06-27 00:00:00.000;5,31
and I try to use Python's CSV tools to parse it but cannot understand the paragraph, source:
And while the module doesn’t directly support parsing strings, it can easily be done:
import csv
for row in csv.reader(['one,two,three']):
print row
Could someone clarify the line ['one,two,three']? How would you use it with format A;B;C?
The greenlet package is used by gevent and eventlet for asynchronous IO. It is written as a C-extension and therefore doesn't work with Jython or IronPython. If performance is of no concern, what is the easiest approach to implementing the greenlet API in pure Python.
A simple example:
def test1():
print 12
gr2.switch()
print 34
def test2():
print 56
gr1.switch()
print 78
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()
Should print 12, 56, 34 (and not 78).
Hi
I wrote a matlab code (that easily could be implimented as a function) that convert a series of BMPs to avi. I want a python program to call to this program/function. how do I do it?
thanks
I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case:
I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do nothing and continue on.
Which way is better?
try:
A["blah"] = B["blah"]
except KeyError:
pass
or
if "blah" in B:
A["blah"] = B["blah"]
"Do and ask for forgiveness" vs. "simplicity and explicitness".
Which is better and why?
I am pretty new to python and I discovered by myself that I can create a list of function and call with a for loop.
example:
def a(args):
print "A"
def b(args):
print "B"
def c(args):
print "C " + str(args)
functions = [a,b,c]
for i in functions:
i(1)
So, my question is: is there any good practice or elegant way to use list of functions and what is a good use of all this? (do have a particular name the "list of functions"?)
thank you
Hi folks,
I'm having quite a problem deciding how to serve a few Python scripts.
The problem is that the basic functionality could be generalized by this:
do_something()
time.sleep(3)
do_something()
I tried various WSGI servers, but they have all been giving me concurrency limitations, as in I have to specify how many threads to use and so on.
I only wish that the resources on the server be used efficiently and liberally.
Any ideas?
Hi,
I need to compare two files and redirect the different lines to third file. I know using diff command i can get the difference . But, is there any way of doing it in python ? Any sample code will be helpful
Hey, here's a dumb question: how can I set an object property given its name in a string. I have a dictionary being passed to me and I wish to transfer its values into namesake properties using code like this:
for entry in src_dict:
if entry.startswith('can_'):
tgt_obj[entry] = src_dict_profile[entry]
I'm still a bit of a noob with Python so would appreciate some help.
- dave.
I'm looking for a way in python to find out which type of file system is being used for a given path. I'm wanting to do this in a cross platform way. On linux I could just grab the output of df -T but that won't work on OSX or windows.
Hello everyone, i'm looking for a way in python to run an external binary and watch it's output for: "up to date" If "up to date" isn't returned i want to run the original command again, once "up to date" is displayed i would like to be able to run another script. So far I've figured out how to run the binary with options using subprocess but thats as far as I've gotten. Thanks!
Hi
I am trying to perform a 2d convolution in python using numpy
I have a 2d array as follows with kernel H_r for the rows and H_c for the columns
data = np.zeros((nr, nc), dtype=np.float32)
#fill array with some data here then convolve
for r in range(nr):
data[r,:] = np.convolve(data[r,:], H_r, 'same')
for c in range(nc):
data[:,c] = np.convolve(data[:,c], H_c, 'same')
It does not produce the output that I was expecting, does this code look OK
Thanks
I'm trying to use Emacs as a python editor and it works fine when I evaluate(C-c C-c) only single files but when I evaluate a file that imports another file in the same directory, I get an error saying that the file could not be imported.
Does anyone know of a workaround?
Thanks in advance
I downloaded a webpage in my python script.
In most cases, this works fine.
However, this one had a response header: GZIP encoding, and when I tried to print the source code of this web page, it had all symbols in my putty.
How do decode this to regular text?