I have follow input data:
input_data = [
('obj1', 0),
('obj2', 0),
('obj3', 0),
('obj22', 0),
('obj4', 1),
('obj5', 1),
('obj6', 1),
('obj7', 2),
('obj8', 2),
('obj9', 3),
('obj10', 3),
]
I need to write an algorithm that will make permutations by 2 but except items where position (second element in tuple) for a left item will >= position of a right item or less than 1.
Example output:
expected = [
[('obj1', 0), ('obj4', 1)],
[('obj1', 0), ('obj5', 1)],
[('obj1', 0), ('obj6', 1)],
[('obj2', 0), ('obj4', 1)],
[('obj2', 0), ('obj5', 1)],
[('obj2', 0), ('obj6', 1)],
[('obj3', 0), ('obj4', 1)],
[('obj3', 0), ('obj5', 1)],
[('obj3', 0), ('obj6', 1)],
[('obj22', 0), ('obj4', 1)],
[('obj22', 0), ('obj5', 1)],
[('obj22', 0), ('obj5', 1)],
[('obj4', 1), ('obj7', 2)],
[('obj4', 1), ('obj8', 2)],
[('obj5', 1), ('obj7', 2)],
[('obj5', 1), ('obj8', 2)],
[('obj6', 1), ('obj7', 2)],
[('obj6', 1), ('obj8', 2)],
[('obj7', 2), ('obj9', 3)],
[('obj7', 2), ('obj10', 3)],
[('obj8', 2), ('obj9', 3)],
[('obj8', 2), ('obj10', 3)],
]
CodePudding user response:
If I got your question right, below would be your answer:
from itertools import permutations
input_data = [
('obj1', 0),
('obj2', 0),
('obj3', 0),
('obj22', 0),
('obj4', 1),
('obj5', 1),
('obj6', 1),
('obj7', 2),
('obj8', 2),
('obj9', 3),
('obj10', 3),
]
perm = permutations(input_data, 2)
expected = []
for p in perm:
if p[0][1] < p[1][1]:
expected.append([p[0], p[1]])
Result:
expected = [
[('obj1', 0), ('obj4', 1)]
[('obj1', 0), ('obj5', 1)]
[('obj1', 0), ('obj6', 1)]
[('obj1', 0), ('obj7', 2)]
[('obj1', 0), ('obj8', 2)]
[('obj1', 0), ('obj9', 3)]
[('obj1', 0), ('obj10', 3)]
[('obj2', 0), ('obj4', 1)]
[('obj2', 0), ('obj5', 1)]
[('obj2', 0), ('obj6', 1)]
[('obj2', 0), ('obj7', 2)]
[('obj2', 0), ('obj8', 2)]
[('obj2', 0), ('obj9', 3)]
[('obj2', 0), ('obj10', 3)]
[('obj3', 0), ('obj4', 1)]
[('obj3', 0), ('obj5', 1)]
[('obj3', 0), ('obj6', 1)]
[('obj3', 0), ('obj7', 2)]
[('obj3', 0), ('obj8', 2)]
[('obj3', 0), ('obj9', 3)]
[('obj3', 0), ('obj10', 3)]
[('obj22', 0), ('obj4', 1)]
[('obj22', 0), ('obj5', 1)]
[('obj22', 0), ('obj6', 1)]
[('obj22', 0), ('obj7', 2)]
[('obj22', 0), ('obj8', 2)]
[('obj22', 0), ('obj9', 3)]
[('obj22', 0), ('obj10', 3)]
[('obj4', 1), ('obj7', 2)]
[('obj4', 1), ('obj8', 2)]
[('obj4', 1), ('obj9', 3)]
[('obj4', 1), ('obj10', 3)]
[('obj5', 1), ('obj7', 2)]
[('obj5', 1), ('obj8', 2)]
[('obj5', 1), ('obj9', 3)]
[('obj5', 1), ('obj10', 3)]
[('obj6', 1), ('obj7', 2)]
[('obj6', 1), ('obj8', 2)]
[('obj6', 1), ('obj9', 3)]
[('obj6', 1), ('obj10', 3)]
[('obj7', 2), ('obj9', 3)]
[('obj7', 2), ('obj10', 3)]
[('obj8', 2), ('obj9', 3)]
[('obj8', 2), ('obj10', 3)]
]