[Tutor] A program that can check if all elements of the list are mutually disjoint
Manprit Singh
manpritsinghece at gmail.com
Sun Jun 6 04:56:55 EDT 2021
Dear sir ,
Sorry i have written lst in place of arr:
correct function is
import itertools
def is_mutually_disjoint(arr):
comb = itertools.combinations(arr, 2)
return all(set(ele1).isdisjoint(ele2) for ele1, ele2 in comb)
Kindly comment now .....
On Sun, Jun 6, 2021 at 2:21 PM Peter Otten <__peter__ at web.de> wrote:
> On 06/06/2021 10:19, Manprit Singh wrote:
> > Dear sir ,
> > Thanks to Roel . Finally today I have understood the importance of
> standard
> > library modules, I work with mostly numbers when programming , and feel
> > that itertools is very very useful.
> > The solution that i do agree is as follows and seems readable to me too:
> >
> > import itertools
> > def is_mutually_disjoint(arr):
> > comb = itertools.combinations(lst, 2)
> > return all(set(ele1).isdisjoint(ele2) for ele1, ele2 in comb)
>
> Note that there's a bug in the above function (try running it with a
> list literal) giving you a chance to learn why global variables should
> be avoided ;)
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list