문제

I have an array of:

filename,number of times to show it

Example:

video1.mp4,50
video2.mp4,100
video3.mp4,50
video4.mp4,150

What's the best way to create a playlist file, like:

video4
video2
video4
video1
video2
video3
video4

Maybe smth like:

take max show_times variable = it is a number of show blocks and try to make each block like:

video_files_list = ((file1,50),(file2,100),(file3,300))

playlist = []

for i =0 to max_show_times: // max_show_times = 300

for k in video_files_list:

  if i % (max_show_times/(max_show_times/k[1])) ==0:  

// k[1] - for file1 is 50, for file2 is 100

         playlist.add(k[0])
도움이 되었습니까?

해결책

You algorithm can be implemented in a such way

from itertools import imap

def generate_playlist(pairs):
  '''
  @param pairs: list of pairs, first element is a file name, second - repetition amount
  @return: generator for playlist items
  '''
  max_show_times = max(imap(second, pairs))
  for i in xrange(max_show_times):
    for file_path, repetition_count in pairs:
      if i % (max_show_times / repetition_count) == 0:  
           yield file_path

def second(col):
  if col and len(col) > 1:
    return col[1]

playlist = generate_playlist(file_to_count_pairs)
playlist = imap(get_file_name_without_ext, playlist)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top