Python Forum
How to create subset in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create subset in python?
#1
Hi, I have a query about how to create a list of subsets from a given list. I tried doing it but I couldn't get the correct output.
For example, if the given list is [1,4,2,7], then if I take index-based, these are the subsets I need [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)].
Can you please help me with the logic to find subsets?
Reply
#2
Could you please explain the logic of how you get [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] from [1,4,2,7]
https://en.wikipedia.org/wiki/Subset Wrote:Subset
In mathematics, set A is a subset of a set B if all elements of A are also elements of B
There are elements in your A that are not in your B.
Reply
#3
Sorry for not giving enough clarity. The subsets I mentioned are tthe indexes of elements from given list.
Reply
#4
I wouldn't call those subsets. Look at itertools.collections
Bhavika likes this post
Reply
#5
(Nov-27-2021, 03:03 PM)deanhystad Wrote: I wouldn't call those subsets. Look at itertools.collections

Thank you. I will look into it.
Reply
#6
>>> n = 4
>>> [(i, j) for i in range(n) for j in range(i+1, n)]
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  bug in subset sum algorithm usercat123 3 1,386 Feb-07-2022, 05:41 AM
Last Post: deanhystad
  how to refer to a subset of a list Skaperen 4 1,653 Jan-16-2022, 02:16 AM
Last Post: Skaperen
  Grabbing a Subset of a String acemurdoc 3 2,601 Jun-18-2019, 04:57 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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