我正在使用python2.7。我有一个包含染色体位置和实验ID的文件。我有存储在时刻两个列表这样的信息:创建一个与列表关联的字典并通过一个循环更新它
unique_locations - containing a single value for each location
location_exp - containing lists of [location, experiment]
我没有用字典的原因是,有多个实验中发现多个地点 - 即这是一个多对多的关系。
我想知道每个位置的实验数量。即得到这样的列表:
[
[location1, [experiment1, experiment2, experiment3]],
[location2, [experiment2, experiment3, experiment4]]
]
等
由于列表的长度是不同的我一直在使用上对于列的枚举(列表)循环失败。我曾尝试:
location_experiment_sorted = []
for i, item in enumerate(unique_experiment):
location = item[0]
exp = item[1]
if location not in location_experiment_sorted:
location_experiment_sorted.append([location, exp])
else:
location_experiment_sorted[i].append(exp)
其中包括其他事情。我也尝试使用与多个实验列表相关的字典。任何人都可以将我指向正确的方向吗?
我想你可以简单地对你的'location_exp'列表进行排序,然后使用['itertools.groupby'](https://docs.python.org/2/library/itertools.html#itertools.groupby)。不知道我是否正确理解了这种情况。 –