This question already has answers here: How do I iterate through two lists in parallel? (8 answers) How to find list intersection? (16 answers) Closed 5 months ago. I want to take two lists and find the values that appear in both. would return [5], for instance. Answer Not the most efficient one, but by far t…
Tag: python
String formatting
I have a list filter = [‘a’, ‘b’, ‘c’]. I need to frame the following string out of the list “item -a item -b item -c”. Which is the most efficient way to do this? Usually the list filter contains 100 to 200 items and each would be of length 100 – 150. Wou…
Add variables to tuple
I am creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. I am taking information from the user and store it in variables. Can I add these variables into a tuple? Can you please help me with the syntax? I only need
Python dicts in sqlalchemy
I would like to load/save a dict to/from my sqlite DB, but am having some problems figuring out a simple way to do it. I don’t really need to be able to filter, etc., based on the contents so a simple conversion to/from string is fine. The next-best thing would be foreign keys. Please don’t post l…
virtualenv in PowerShell?
There seems to be a problem when virtualenv is used in PowerShell. When I try to activate my environment in PowerShell like… .. nothing happens. (the shell prompt should have changed as well as the PATH env. variable .) I guess the problem is that PowerShell spawns a new cmd. process just for running th…
AttributeError: ‘NoneType’ object has no attribute ‘GetDataStore’
I guys, I developing a utility in python and i have 2 object the main class and an database helper for get sqlserver data. database.py gaemodel.py My problem is when i try run code return me this error What is wrong ?? Answer First of all: The __new__ method should be named __init__. Remove the global _host e…
Extract part of a regex match
I want a regular expression to extract the title from a HTML page. Currently I have this: Is there a regular expression to extract just the contents of <title> so I don’t have to remove the tags? Answer Use ( ) in regexp and group(1) in python to retrieve the captured string (re.search will return…
Python unit test with base and sub class
I currently have a few unit tests which share a common set of tests. Here’s an example: The output of the above is: Is there a way to rewrite the above so that the very first testCommon is not called? EDIT: Instead of running 5 tests above, I want it to run only 4 tests, 2 from the SubTest1 and
Should I use ‘has_key()’ or ‘in’ on Python dicts? [duplicate]
This question already has answers here: Check if a given key already exists in a dictionary (16 answers) Closed last month. Given: Which of the following is the best way to check if ‘a’ is in d? Answer in is definitely more pythonic. In fact has_key() was removed in Python 3.x.
In Python, how to check if a string only contains certain characters?
In Python, how to check if a string only contains certain characters? I need to check a string containing only a..z, 0..9, and . (period) and no other character. I could iterate over each character and check the character is a..z or 0..9, or . but that would be slow. I am not clear now how to do it with