Python 中的分组列表

Grouping Lists in Python

我正在尝试根据公共索引将多个列表组合在一起。公共索引是 Julian Date (JD),我想将共享相同 JD 的其他列表中的所有其他时间合并到包含单个 JD 和所有相应时间的单个列表中。但是,我一直收到 "TypeError: list indices must be integers, not tuple",我不确定为什么会这样。我最初不是为变量 "list_of_lsts" 分配一个实际的列表列表,还是我在循环中的某个地方搞砸了。我的最终结果如下:

['2014-158', '20:07:11.881', '20:43:04.546', '20:43:47.447', '21:11:08.997', '21:11:16.697', '21:22:07.717']
['2014-163', '17:12:09.071', '17:38:08.219', '17:38:28.310', '17:59:25.649','18:05:59.536', '18:09:53.243','18:13:47.671', '18:16:53.976', '18:20:31.538', '18:23:02.243']

我将对许多 Julian 日期执行此操作,因此循环是必要的。我是一名新程序员,最重要的是 Python 2.7.8 的新手,所以请原谅我的无知和大量错误。谢谢。

lst_of_lsts = (['2014-158', '20:07:11.881', '20:43:04.546']
               ['2014-158', '20:43:47.447', '21:11:08.997']
               ['2014-158', '21:11:16.697', '21:22:07.717']
               ['2014-163', '17:12:09.071', '17:38:08.219']
               ['2014-163', '17:38:28.310', '17:59:25.649']
               ['2014-163', '18:05:59.536', '18:09:53.243']
               ['2014-163', '18:13:47.671', '18:16:53.976']
               ['2014-163', '18:20:31.538', '18:23:02.243'])


def merge_subs(lst_of_lsts):
    res = []
    for row in lst_of_lsts:
        for i, resrow in enumerate(res):
            if row[0]==resrow[0]:
                res[i] += row[1:]
                break
            else:
                res.append(row)
        return res

改为

lst_of_lsts = (['2014-158', '20:07:11.881', '20:43:04.546'],
['2014-158', '20:43:47.447', '21:11:08.997'],
['2014-158', '21:11:16.697', '21:22:07.717'],
['2014-163', '17:12:09.071', '17:38:08.219'],
['2014-163', '17:38:28.310', '17:59:25.649'],
['2014-163', '18:05:59.536', '18:09:53.243'],
['2014-163', '18:13:47.671', '18:16:53.976'],
['2014-163', '18:20:31.538', '18:23:02.243'])

也就是说,您缺少逗号。

我建议的另一个更改:

for i, resrow in enumerate(res):
        if row[0]==resrow[0]:
            res[i] += row[1:]

此处 res[i]resrow 是相同的东西 - 因为改变一个就会改变另一个。没有必要同时拥有它们。相反做

for resrow in res:
    if row[0] == resrow[0]:
        resrow += row[1:]

此外 - 如当前缩进所示,如果 if 失败,则不会执行 else。相反,它将在 for 循环之后完成。我不认为那是你想要的。

不过,你确实想使用字典。

def merge_subs2(lst_of_lsts):
    date_dict = {}
    for row in lst_of_lsts:
        if row[0] not in date_dict:
            date_dict[row[0]] = [row[0]] #note that I'm putting row[0] here already.
        date_dict[row[0]].extend(row[1:])
    returnlist = date_dict.values()
    returnlist.sort()   #sorting because dicts don't have an order to them.
    return returnlist
def merge_subs(L):
    dates = {}
    for sub in L:
        date = sub[0]
        if date not in dates:
            dates[date] = []
        dates[date].extend(sub[1:])
    answer = []
    for date in sorted(dates):
        answer.append([date] + dates[date])
    return answer

输出:

In [28]: lst_of_lsts = (['2014-158', '20:07:11.881', '20:43:04.546'],
['2014-158', '20:43:47.447', '21:11:08.997'],
['2014-158', '21:11:16.697', '21:22:07.717'],
['2014-163', '17:12:09.071', '17:38:08.219'],
['2014-163', '17:38:28.310', '17:59:25.649'],
['2014-163', '18:05:59.536', '18:09:53.243'],
['2014-163', '18:13:47.671', '18:16:53.976'],
['2014-163', '18:20:31.538', '18:23:02.243'])

In [29]: merge_subs(lst_of_lsts)
Out[29]: [['2014-158', '20:07:11.881', '20:43:04.546', '20:43:47.447', '21:11:08.997', '21:11:16.697', '21:22:07.717'], 
          ['2014-163', '17:12:09.071', '17:38:08.219', '17:38:28.310', '17:59:25.649', '18:05:59.536', '18:09:53.243', '18:13:47.671', '18:16:53.976', '18:20:31.538', '18:23:02.243']]