vjeko sa
vjeko sa

Reputation: 1

Tupe video clips list, the other matching durations. Itertools can find triplets that total 90 sec from duration list. Want to print clip names

I can get the triplets with iteration tools in durations list but I want to get matching clips from spots list. Tried to marry two list but dont know how to apply iteration combo on one part of the pair. Can I copy index enumeration from the 'result' and print matching clips?

from itertools import combinations

spots = ("clip1", "clip2", "clip3", "clip4", "clip5", "clip6", "clip7", "clip8", "clip9", "clip10")
durations = (30, 15, 5, 15, 30, 15, 15, 60, 25, 60)
spot_dur = list(zip(spots, durations))
#print(spot_dur)

#def find_triplets(spot_dur, target):
def find_triplets(durations, target):
   triplets = []
   #for combo in combinations(spot_dur, 3):  #error unsupported operand type(s) for +: 'int' and 'tuple'
   for combo in combinations(durations, 3):
      if sum(combo) == target:    
         triplets.append(combo)
   return triplets


target = 90
#result = find_triplets(spot_dur, target)
result = find_triplets(durations, target)
print(result)

for index, item in enumerate(spot_dur):
    print(f"{item}: {result[index]}")

result when I tried index enumerating 'result'

('clip1', 30): (15, 15, 60)
('clip2', 15): (15, 15, 60)
('clip3', 5): (15, 15, 60)
('clip4', 15): (15, 15, 60)
('clip5', 30): (15, 15, 60)
('clip6', 15): (15, 15, 60)
('clip7', 15): (5, 60, 25)
('clip8', 60): (5, 25, 60)
('clip9', 25): (15, 15, 60)

What I want is (for example)...

(clip1, clip4, clip6)
(clip6, clip7, clip2)
(clip7, clip6, clip9)
....

Upvotes: -1

Views: 25

Answers (1)

Barmar
Barmar

Reputation: 782168

Change find_triplets() to just sum the durations component of spot_dur.

def find_triplets(spot_dur, target):
   triplets = []
   for combo in combinations(spot_dur, 3):
      if sum(x[1] for x in combo) == target:    
         triplets.append(tuple(x[0] for x in combo))
   return triplets

Upvotes: 0

Related Questions