All permutations from 2 lists
Rob Cliffe
rob.cliffe at btinternet.com
Thu Mar 3 09:43:28 EST 2022
On 03/03/2022 14:07, Larry Martell wrote:
> On Wed, Mar 2, 2022 at 9:42 PM Avi Gross via Python-list
> <python-list at python.org> wrote:
>> Larry,
>>
>> i waited patiently to see what others will write and perhaps see if you explain better what you need. You seem to gleefully swat down anything offered. So I am not tempted to engage.
> But then you gave in to the temptation.
>
>> And it is hard to guess as it is not clear what you will do with this.
> In the interests of presenting a minimal example I clearly
> oversimplified. This is my use case: I get a dict from an outside
> source. The dict contains key/value pairs that I need to use to query
> a mongodb database. When the values in the dict are all scalar I can
> pass the dict directly into the query, e.g.:
> self._db_conn[collection_name].find(query). But if any of the values
> are lists that does not work. I need to query with something like the
> cross product of all the lists. It's not a true product since if a
> list is empty it means no filtering on that field, not no filtering on
> all the fields. Originally I did not know I could generate a single
> query that did that. So I was trying to come up with a way to generate
> a list of all the permutations and was going to issue a query for each
> individually. Clearly that would become very inefficient if the lists
> were long or there were a lot of lists. I then found that I could
> specify a list with the "$in" clause, hence my solution.
>
>> def query_lfixer(query):
>> for k, v in query.items():
>> if type(v)==list:
>> query[k] = {"$in": v}
>> return query
>>
>> self._db_conn[collection_name].find(query_lfixer(query))
>>
>>
>> So why did so many of us bother?
> Indeed - so why did you bother?
You started with a request for help that did not say what you actually
wanted.
When people took the time and trouble to give you answers to the
question **as posed**, you repeatedly rejected them while still being
unclear about what you actually wanted.
Now you seem to be being rude to Avi.
There are plenty of people willing to help on this list, and plenty of
goodwill to those that need help - but goodwill can be used up if it is
abused.
Respectfully,
Rob Cliffe
More information about the Python-list
mailing list