Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Are we running light with overbyte?


devel / comp.lang.python / Re: All permutations from 2 lists

SubjectAuthor
o Re: All permutations from 2 listsAvi Gross

1
Re: All permutations from 2 lists

<mailman.110.1646275325.2329.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17285&group=comp.lang.python#17285

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: avigr...@verizon.net (Avi Gross)
Newsgroups: comp.lang.python
Subject: Re: All permutations from 2 lists
Date: Thu, 3 Mar 2022 02:42:00 +0000 (UTC)
Lines: 55
Message-ID: <mailman.110.1646275325.2329.python-list@python.org>
References: <1388685953.247061.1646275320253.ref@mail.yahoo.com>
<1388685953.247061.1646275320253@mail.yahoo.com>
Reply-To: Avi Gross <avigross@verizon.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de 6PvjqbnQS6NaDOJ6w+9xtwMiyoHnWgMbJ+9nwDO6NBOQ==
Return-Path: <avigross@verizon.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=verizon.net header.i=@verizon.net header.b=Mu+Z4s2G;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.029
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'def': 0.04; 'random': 0.05; 'filter': 0.07; 'modules':
0.07; 'suggestion': 0.07; 'describe': 0.09; 'enough.': 0.09;
'module:': 0.09; 'pandas': 0.09; 'supplied': 0.09; 'trivial':
0.09; 'questions.': 0.14; 'arbitrary': 0.16; 'arguments': 0.16;
'as-is': 0.16; 'columns': 0.16; 'combines': 0.16; 'dictionary,':
0.16; 'empty,': 0.16; 'received:74.6.131': 0.16; 'sets,': 0.16;
'sounds': 0.16; 'subject:lists': 0.16; 'treats': 0.16; 'tuple':
0.16; 'tuples': 0.16; 'tuples,': 0.16; 'tuples.': 0.16; 'url-
ip:104.26.0.204/32': 0.16; 'url-ip:104.26.1.204/32': 0.16; 'url-
ip:172.67.71.236/32': 0.16; 'url:17': 0.16; 'url:pandas': 0.16;
'url:pydata': 0.16; 'python': 0.16; 'applications': 0.17; 'it?':
0.19; 'to:addr:python-list': 0.20; 'languages': 0.22;
'communicate': 0.22; 'maybe': 0.22; 'returns': 0.22; 'way.': 0.22;
'code': 0.23; 'to:name:python-list@python.org': 0.24; 'anything':
0.25; 'help.': 0.25; 'seems': 0.26; 'function': 0.27; 'done':
0.28; 'example,': 0.28; 'suggest': 0.28; 'error': 0.29; 'seem':
0.31; 'approach': 0.31; 'looked': 0.31; 'question': 0.32;
'assume': 0.32; 'checks': 0.32; 'empty': 0.32; 'guess': 0.32;
'point,': 0.32; 'specified': 0.32; 'structure': 0.32; 'but': 0.32;
'there': 0.33; 'able': 0.34; 'fine': 0.35; 'yes,': 0.35; 'lists':
0.37; 'subject:from': 0.37; 'using': 0.37; 'hard': 0.37; 'others':
0.37; 'this.': 0.37; 'could': 0.38; 'two': 0.39; 'quite': 0.39;
'single': 0.39; 'valid': 0.39; 'list': 0.39; 'methods': 0.39;
'explain': 0.40; 'match': 0.40; 'true.': 0.40; 'both': 0.40;
'something': 0.40; 'want': 0.40; 'above': 0.62; 'data,': 0.63;
'once': 0.63; 'pass': 0.64; 'url-ip:104.26/16': 0.64; 'willing':
0.64; 'clear': 0.64; 'down': 0.64; 'deliver': 0.64; 'remains':
0.64; 'requirement': 0.64; 'your': 0.64; 'similar': 0.65; 'well':
0.65; 'types': 0.67; 'worked': 0.67; 'back': 0.67; 'generally':
0.67; 'right': 0.68; 'items': 0.68; 'order': 0.69; 'and,': 0.69;
'generator': 0.69; 'perfectly': 0.69; 'above,': 0.70; 'care':
0.71; 'future': 0.72; 'deal': 0.73; 'easy': 0.74; 'url-
ip:104.26.0/24': 0.76; 'url-ip:104.26.1/24': 0.76; 'out,': 0.78;
'header:Reply-To:1': 0.79; 'happens': 0.84; 'need.': 0.84;
'combinations': 0.84; 'converted': 0.84; 'nan': 0.84; 'remainder':
0.84; 'stating': 0.84; 'subject:All': 0.84; 'subtle': 0.84;
'waited': 0.84; 'want.': 0.84; 'earn': 0.91; 'preserved': 0.91;
'return.': 0.91; 'time?': 0.91; 'skip:q 20': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=verizon.net; s=a2048;
t=1646275322; bh=YxebZRXNK0NR9SEVKJRFyvqq43/9qUPj+pTvXSV7qM0=;
h=Date:From:Reply-To:To:Subject:References:From:Subject:Reply-To;
b=Mu+Z4s2GEZZ1FiICS5uq1uo88EbCNyv5Fx0sNksmZqnY1h3kHxphjECdkMBNKW1+hVHumIzeUc9XaSOuHkJgbS04kt35G41aWbdYgw4JVZU3qeFql1NL2Xk+04I2SYtdL9FdNrSavaUt02vdI5hw4JTE0ZG78XewA453ROCE07hwNRQC4IPavErXAGEK6CFaPT6CN2++ihiEtXAz/kW5AGqrmPfvGul2Ex4V9RM70PwQqapk25s9bz1Y2WtPr0eJ3Txljqn5uUTs3jt41XxFkN2S2HfJfAA6ZgAnNYicmX0heMXWkiE4UUzTdNuEAbiUVxhPo2SGWGjcN8dHXu95jw==
X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048;
t=1646275322; bh=8WPJKWd7kE/B+63YDJuK6FQQzFJakC3XPbXFIlTBgUC=;
h=X-Sonic-MF:Date:From:To:Subject:From:Subject;
b=oNUHiGDh/CUJjpML0lkx0Pbfy9BgUk6j4K1LOp0F49heMoXzomKxhJ3eDu1/EWypla0BiLwXkfI/pzdiiL7pbCeYoC2aTCkk40fpPXjz2nRznOsnKWPzWLLsi8djf25iy8t/Om9Hy6JbrDthlWkrm0nRrF0MVpAtshq2lZbdwfYZLYA+Dd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zyzEWbMaUiq2JcEHKXgr6wz5hFhxE7rVpHw6lQGLzj_KeRPzcPZazGwjCM2arCq1ZTR04OFVbe
fKHezgqglR6cz_B2XAgprNqrkbgmETfk9cdGYpx4ei7qXjwlLh5vdO9w4jNqeE8xFvszQdZK8V6Y
aycQhlM7GWfkbulFXRweXOr4yOHv.XZ5lIxBKdo6jdasrnEdUpBBNeqGd1WQ.15uxsCLYKmnCKI_
X4ipXrfKMAB_z5jqEUuDYmFdnC53zgvQs.YEj0qysKoCEcNkPYIPsgPhO.yZojxXWbA--
X-Sonic-MF: <avigross@verizon.net>
X-Mailer: WebService/1.1.19724 aolwebmail
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <1388685953.247061.1646275320253@mail.yahoo.com>
X-Mailman-Original-References: <1388685953.247061.1646275320253.ref@mail.yahoo.com>
 by: Avi Gross - Thu, 3 Mar 2022 02:42 UTC

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.

Some later messages suggest you may not be specifying quite what you want. It sounds like you are asking for something like a function that combines two things a certain way. Initially it sounded like two simple lists of any length and something like this was proposed:

[(f, s) for f in os for s in region]

It returns a list of tuples, but could just as easily return a list of lists or dictionary or other variants.

You pointed out, validly, that if either of the original lists is empty, it fails. True.

But that opens the question of what happens when any random thing is thrown there such as "hello" which Python happily treats as ['h', 'e', 'l', 'l', 'o'] for this purpose. You now seem to want a bulletproof solution that might start with a very basic function like this but add more checks and deal appropriately:

def combine_2(first, second):
l_first = len(first)
l_second = len(second)

if ( l_first + l_second == 0 ): return([])
if ( l_first == 0 ): return(second)
if ( l_second == 0 ): return(first)
return([(f, s) for f in first for s in second])

The above works fine if I give it [] for one or both but maybe not. It returns an empty list if both are empty, which may be what you want or you may want an error or an empty tuple, ...

You wanted tuples in return. But if either one is empty, the above returns the original, a list without tuples. To return the valid list with each element as a singleton tuple could easily be done but you have not initially specified the expected behavior.

And it is hard to guess as it is not clear what you will do with this. Do you want to be able to do this for more than 2 at a time? At some point, the above approach is not right and an approach that works for an arbitrary number of lists to combine may make sense. Some python modules do something similar to the R function expand.grid() that makes all combinations into rows of a data.frame such as this expand_grid in the pandas module:

https://pandas.pydata.org/pandas-docs/version/0.17.1/cookbook.html#creating-example-data

I assume that could be used as-is in some applications but could also be easily converted back into a list of tuples or have a generator deliver one result each time ...

And, yes, the above approach needs to convert your arguments into a single dictionary but that is easy enough. Getting back a list of lists horizontally may be something like this:

df.values.tolist()

As others have pointed out, you are actually not stating what you want and are getting the right answer if you ALLOW any of your items to be empty. It is like any operation using an NaN or an Inf do not work well as the result is generally nonsense. If you do not want any empty sets, take care to exclude them. In my dictionary example above, you might add only non-empty lists to the dictionary, for example, and if the dictionary remains empty, then there is nothing to combine.

I also see a subtle requirement that is annoying. You want the NAME of each entry preserved in order even as some may be EMPTY. So i ask where these names are stored so you can communicate them. This is trivial in languages like R but perhaps not in Python without some help. My suggestion is that numpy/pandas and their data types can help. Once you make a structure like I describe above, the columns have headers you can ask about and then you can pass the headers of surviving entities that perfectly match the columns, even if you take the columns out into a list of tuples.

It looks to some like you want lots of things without being willing to earn them. Any number of methods will easily remove or filter what you keep. Once you have clean data, the remainder can be trivial.

You then supplied a solution you said worked fine that looked pretty much NOTHING like what we have been discussing:

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))

The above code seems be taking a dictionary and modifying it? And what you needed in the end was a dictionary? So why did so many of us bother? I will keep that in mind for future questions.


devel / comp.lang.python / Re: All permutations from 2 lists

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor