I have two lists. The first list holds the names of points on a line. The second list holds the ordered distances between those points.
points = ['A', 'B', 'C', 'D']
segment_lengths = [10, 20, 30]
Examples
- Segment lengths for the point group (A, B) is (10). Segment lengths for the point group (B,C) is (20). Segment lengths for the point group (C, D) is (30)
- Segment lengths for the point group (A, C) is (10, 20)
- Segment lengths for the point group (A, D) is (10, 20, 30)
- Segment lengths for the point group (A, B, C) is (10, 20)
- Segment lengths for the point group (A, B, C, D) is (10, 20, 30)
Problem
I need to do two things:
- Extract all possible ordered combinations of point groups from list
points - Create matching segment length combinations from list
segment_lengths
Desired Output
So, the desired output from list a is:
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'C')
('B', 'D')
('C', 'D')
('A', 'B', 'C')
('A', 'B', 'D')
('A', 'C', 'D')
('B', 'C', 'D')
('A', 'B', 'C', 'D')
desired matching output from list b:
(10)
(10, 20)
(10, 20, 30)
(20)
(20, 30)
(30)
(10, 20)
(10, 20, 30)
(10, 20, 30)
(20, 30)
(10, 20, 30)
Trials so far
I was able to create the first output using itertools as follows:
for i in range(2, len(points)+1):
a = itertools.combinations(points, i)
for elem in a:
print(elem)
However, I am unable find a good way to create the matching output from the second list.
for i in range(1, len(segment_lengths)+1):
b = itertools.combinations(segment_lengths, i)
for elem in b:
print(elem)
creates the following:
(10,)
(20,)
(30,)
(10, 20)
(10, 30)
(20, 30)
(10, 20, 30)
which are the correct possibilities, but obviously this is not mapped to the first output.
I was wondering if there is an elegant and clean way creating the second output using itertools or some other built-in library. If not I think I would probably need to go the route of for loops and indices (which I would like to avoid if I can).
Thank you for the help!
EDIT: Made some corrections to the code where I was using inconsistent list names.