Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A list of lists
#6
(Feb-25-2019, 04:14 PM)ichabod801 Wrote: As Larz said, this functionality is all covered in Python. But if you need to recreate it for homework, to get the subsets, start with a list of the empty set. Loop through the items in the set, adding to the list of subsets every subset plus the item:

>>> subsets = [[]]
>>> fullset = [1, 2, 3]
>>> for item in fullset:
...    subsets = subsets + [subset + [item] for subset in subsets]
...
>>> subsets
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
You should be able to modify that to get the list of subsets you want.

Thank you ichabod801, this works ! I will use your trick.
But why didn't it work in my program ?

Thank you also for your suggestion about the intersections.
Reply


Messages In This Thread
A list of lists - by Hassediagram - Feb-25-2019, 01:38 PM
RE: A list of lists - by Larz60+ - Feb-25-2019, 03:40 PM
RE: A list of lists - by Hassediagram - Feb-25-2019, 04:13 PM
RE: A list of lists - by ichabod801 - Feb-25-2019, 04:08 PM
RE: A list of lists - by ichabod801 - Feb-25-2019, 04:14 PM
RE: A list of lists - by Hassediagram - Feb-25-2019, 04:23 PM
RE: A list of lists - by ichabod801 - Feb-25-2019, 04:34 PM
RE: A list of lists - by Hassediagram - Feb-25-2019, 04:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 953 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,106 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,128 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,727 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,188 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,926 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,159 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 3,433 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 12,258 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,233 Nov-13-2020, 04:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020