1

我正在寻找有关如何以最快的方式解析单个列表、使用两个嵌套循环、避免进行len(list)^2比较以及避免组中重复文件的建议。

更准确地说:我有一个“文件”对象列表,每个对象都有一个时间戳。我想按文件的时间戳和时间偏移对文件进行分组。前任。从文件 X 开始,我想创建一个包含所有具有timestamp < (timestamp(x) + offset).

为此,我做了:

for file_a in list:
   temp_group = group()
   temp_group.add(file_a)
   list.remove(file_a)
   for file_b in list:
      if (file_b.timestamp < (file_a.timestamp + offset)):
         temp_group.add(file_b)
         list.remove(file_b)

   groups.add(temp_group)

(好吧,代码更复杂,但这是主要思想)

这显然不起作用,因为我在循环期间修改了列表,并且发生了奇怪的事情:)

我以为我必须为循环使用“列表”的副本,但是,这也不起作用:

for file_a in list[:]:
   temp_group = group()
   temp_group.add(file_a)
   list.remove(file_a)
   for file_b in list[:]:
      if (file_b.timestamp < (file_a.timestamp + offset)):
         temp_group.add(file_b)
         list.remove(file_b)

   groups.add(temp_group)

好吧..我知道我可以在不从列表中删除元素的情况下做到这一点,但是我需要标记那些已经“处理”过的元素,并且每次都需要检查它们——这是一种速度损失。

谁能给我一些关于如何以最快/最好的方式做到这一点的建议?

谢谢,

亚历克斯

编辑:我找到了另一个解决方案,它不能完全回答问题,但它是我真正需要的(我以这种方式提出问题的错误)。我在这里发布这个是因为它可以帮助人们在 Python 中寻找与列表循环相关的问题。

它可能不是最快的(考虑到通过列表的“通过”次数),但它很容易理解和实现,并且不需要对列表进行排序。

我避免排序的原因是它可能需要更多时间,因为在我制作了第一组组后,其中一些将被“锁定”,而未锁定的组将被“解散”,并使用不同的时间偏移。(并且在解散组时,文件顺序可能会改变,它们将需要重新排序)。

无论如何,解决方案是自己控制循环索引。如果我从列表中删除一个文件,我会跳过增加索引(例如:当我删除索引“3”时,之前的索引“4”现在是“3”,我不想增加循环计数器,因为我会跳过它)。如果在那次迭代中我没有删除任何项目,那么索引会正常增加。这是代码(有一些额外的;忽略所有那些“桶”的东西):

def regroup(self, time_offset):
    #create list of files to be used for regrouping
    regroup_files_list = []

    if len(self.groups) == 0:
        #on first 'regroup', we start with a copy of jpeg_list, so that we do not change it further on
        regroup_files_list = copy.copy(self.jpeg_list) 

    else:
        i = 0
        while True:
            try:
                group = self.groups[i]
            except IndexError:
                break

            if group.is_locked == False:
                regroup_files_list.extend(group)                    
                self.groups.remove(group)
                continue
            else:
                i += 1

    bucket_group = FilesGroup()
    bucket_group.name = c_bucket_group_name

    while len(regroup_files_list) > 0: #we create groups until there are no files left
        file_a = regroup_files_list[0]
        regroup_files_list.remove(file_a)

        temp_group = FilesGroup()
        temp_group.start_time = file_a._iso_time
        temp_group.add(file_a)

        #manually manage the list index when iterating for file_b, because we're removing files
        i = 0

        while True:
            try:
                file_b = regroup_files_list[i]
            except IndexError:
                break

            timediff = file_a._iso_time - file_b._iso_time              
            if timediff.days < 0 or timediff.seconds < 0:
                timediff = file_b._iso_time - file_a._iso_time

            if timediff < time_offset:
                temp_group.add(file_b)
                regroup_files_list.remove(file_b)
                continue # :D we reuse the old position, because all elements were shifted to the left

            else:
                i += 1 #the index is increased normally

        self.groups.append(temp_group)

        #move files to the bucket group, if the temp group is too small
        if c_bucket_group_enabled == True:                    
            if len(temp_group) < c_bucket_group_min_count:
                for file in temp_group:
                    bucket_group.add(file)
                    temp_group.remove(file)    
            else:
                self.groups.append(temp_group)      

    if len(bucket_group) > 0:
        self.groups.append(bucket_group)
4

3 回答 3

3

一个简单的解决方案,通过对列表进行排序然后使用生成器创建组来工作:

def time_offsets(files, offset):

   files = sorted(files, key=lambda x:x.timestamp)

   group = []   
   timestamp = 0

   for f in files:
      if f.timestamp < timestamp + offset:
         group.append(f)
      else:
         yield group
         timestamp = f.timestamp
         group = [timestamp]
   else:
      yield group

# Now you can do this...
for group in time_offsets(files, 86400):
   print group

这是一个完整的脚本,您可以运行它来测试:

class File:
   def __init__(self, timestamp):
      self.timestamp = timestamp

   def __repr__(self):
      return "File: <%d>" % self.timestamp

def gen_files(num=100):
   import random
   files = []
   for i in range(num):
      timestamp = random.randint(0,1000000)
      files.append(File(timestamp))

   return files
      

def time_offsets(files, offset):

   files = sorted(files, key=lambda x:x.timestamp)

   group = []   
   timestamp = 0

   for f in files:
      if f.timestamp < timestamp + offset:
         group.append(f)
      else:
         yield group
         timestamp = f.timestamp
         group = [timestamp]
   else:
      yield group

# Now you can do this to group files by day (assuming timestamp in seconds)
files = gen_files()
for group in time_offsets(files, 86400):
   print group
于 2009-10-16T18:59:33.687 回答
1

我能想到的最佳解决方案是O(n log n).

listA = getListOfFiles()
listB = stableMergesort(listA, lambda el: el.timestamp)
listC = groupAdjacentElementsByTimestampRange(listB, offset)

注意groupAdjacentElementsByTimestampRangeO(n).

于 2009-10-16T19:04:08.477 回答
1

我不确定您要做什么 - 在我看来,列表的顺序会影响分组,但您现有的代码可以修改为像这样工作。

#This is O(n^2)
while lst:
    file_a=lst.pop()
    temp_group = group()
    temp_group.add(file_a)
    while lst
        file_b=lst[-1] 
        if (file_b.timestamp < (file_a.timestamp + offset)):
            temp_group.add(lst.pop())
    groups.add(temp_group)

该组是否必须从 file_a.timestamp 开始?

# This is O(n)
from collections import defaultdict
groups=defaultdict(list)  # This is why you shouldn't use `list` as a variable name
for item in lst:
    groups[item.timestamp/offset].append(item)

更简单的方法是分割成具有相似时间戳的组

于 2009-10-16T20:48:08.207 回答