Python Forum

Full Version: How to create subset in python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
Sorry for not giving enough clarity. The subsets I mentioned are tthe indexes of elements from given list.
I wouldn't call those subsets. Look at itertools.collections
(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.
>>> 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)]