我正在使用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)在其他事情中。我还尝试使用与多个实验列表相关的字典。谁能给我指个方向?
发布于 2016-07-26 18:46:26
如果我理解正确(如果位置可以用作字典键)
你可以这样做:
location_experiments={}
for location, experiment in location_exp:
location_experiments.setdefault(location,[]).append(experiment)发布于 2016-07-26 18:47:39
尝试使用defaultdict,即:
from collections import defaultdict
unique_locations = ["location1", "location2"]
location_exp = [
("location1", "experiment1"),
("location1", "experiment2"),
("location1", "experiment3"),
("location2", "experiment2"),
("location2", "experiment3"),
("location2", "experiment4")
]
location_experiment_dict = defaultdict(list)
for location, exp in location_exp:
location_experiment_dict[location].append(exp)
print(location_experiment_dict)将打印输出:
defaultdict(<type 'list'>, {
'location2': ['experiment2', 'experiment3', 'experiment4'],
'location1': ['experiment1', 'experiment2', 'experiment3']
})发布于 2016-07-26 18:47:05
我还没有运行过,如果失败了,很抱歉。如果你说它是一个列表,比如[ location,实验,位置,实验],那么:
locationList = {}
for item in unique_experiment:
location = item[0]
exp = item[1]
if location not in locationList:
locationList[location] = []
locationList[location].append(exp)
else:
locationList[location].append(exp)https://stackoverflow.com/questions/38587083
复制相似问题