我正在尝试根据公共索引将多个列表组合在一起。公共索引是Julian Date(JD),我想将共享相同JD的其他列表中的所有其他时间合并到包含单个JD和所有相应时间的单个列表中。但是,我一直得到一个“TypeError:列表索引必须是整数,而不是元组”,我不知道为什么会发生这种情况。我最初不是将变量“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']
我将为许多朱利安日期做这个,所以循环是必要的。我是一个新的程序员,最重要的是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
答案 0 :(得分:1)
更改为
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
是相同的事情 - 因为更改为1会改变另一个。没有必要同时拥有它们。而是做
for resrow in res:
if row[0] == resrow[0]:
resrow += row[1:]
另外 - 如果if失败,当前缩进的else将不会被完成。相反,它将在for循环之后完成。我不认为这是你想要的。
但实际上,你想使用dict。
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
答案 1 :(得分:1)
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']]