I’m using virtualenv to set up a new project. I installed a lot of things using virtualenv pip from the script folder like below: I have no other packages installed in the global python folder. My code looks like this: and other packages are found like sqlalchemy also installed only in the virtual envir…
Tag: python
python max function using ‘key’ and lambda expression
I come from OOP background and trying to learn python. I am using the max function which uses a lambda expression to return the instance of type Player having maximum totalScore among the list players. The function correctly returns instance of type Player having maximum totalScore. I am confused about the fo…
Access multiple elements of list knowing their index [duplicate]
This question already has answers here: Explicitly select items from a list or tuple (9 answers) Closed 7 months ago. I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, …
Uncrypting a very simple code in python
So this is the brief: i need to input something like: and return: As you can see to decode this i need to start by reading the words in reverse order and if the first letter of the word isupper() then make it lowercase and append it to a list which i will later print. This is what i have
extracting days from a numpy.timedelta64 value
I am using pandas/python and I have two date time series s1 and s2, that have been generated using the ‘to_datetime’ function on a field of the df containing dates/times. When I subtract s1 from s2 s3 = s2 – s1 I get a series, s3, of type timedelta64[ns] How do I look at 1 element of the ser…
Assign a range to a variable
Whenever I try to assign a range to a variable like so: Then try to print the variable: It simply prints ‘range(10, 50)’ instead of all the numbers in the range. Why is this? Answer Thats because range returns a range object in Python 3. Put it in list to make it do what you want:
Efficient calculation of Fibonacci series
I’m working on a Project Euler problem: the one about the sum of the even Fibonacci numbers. My code: The problem’s solution can be easily found by printing sum(list2). However, it is taking a lot of time to come up with the list2 I’m guessing. Is there any way to make this faster? Or is it …
Remove namespace and prefix from xml in python using lxml
I have an xml file I need to open and make some changes to, one of those changes is to remove the namespace and prefix and then save to another file. Here is the xml: I can make the other changes I need, but can’t find out how to remove the namespace and prefix. This is the reusklt xml I
unexpected result in numpy array slicing (view vs copy)
I’m trying to reduce the amount of copying in my code and I came across surprising behavior when dealing with numpy array slicing and views, as explained in: Scipy wiki page on copying numpy arrays I’ve stumbled across the following behavior, which is unexpected for me: Case 1.: As expected, this …
Python zipfile module doesn’t compress files
I have a problem with compression in Python. I know I should call the ZIP_DEFLATED method when writing to make the zip file compressed, but it does not work for me. I have 3 PDF documents in the C:zip directory. When I run the following code it works just fine: It makes the test.zip file without the compressi…