首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个与列表相关联的字典,并通过循环对其进行更新

创建一个与列表相关联的字典,并通过循环对其进行更新
EN

Stack Overflow用户
提问于 2016-07-26 18:15:24
回答 4查看 63关注 0票数 2

我正在使用python2.7。我有一个包含染色体位置和实验ID的文件。目前,我将此信息存储在两个列表中:

代码语言:javascript
复制
unique_locations - containing a single value for each location
location_exp - containing lists of [location, experiment]

我没有使用字典的原因是在多个实验中发现了多个位置-即这是一个多对多的关系。

我想找出每个位置发现了多少个实验。即获取如下列表:

代码语言:javascript
复制
[
    [location1, [experiment1, experiment2, experiment3]], 
    [location2, [experiment2, experiment3, experiment4]]
                                                             ]

等等。

由于列表的长度不同,我在两个列表上使用枚举(列表)循环都失败了。我确实试过了:

代码语言:javascript
复制
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)

在其他事情中。我还尝试使用与多个实验列表相关的字典。谁能给我指个方向?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-07-26 18:46:26

如果我理解正确(如果位置可以用作字典键)

你可以这样做:

代码语言:javascript
复制
location_experiments={}
for location, experiment in location_exp:
    location_experiments.setdefault(location,[]).append(experiment)
票数 2
EN

Stack Overflow用户

发布于 2016-07-26 18:47:39

尝试使用defaultdict,即:

代码语言:javascript
复制
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)

将打印输出:

代码语言:javascript
复制
defaultdict(<type 'list'>, {
    'location2': ['experiment2', 'experiment3', 'experiment4'],
    'location1': ['experiment1', 'experiment2', 'experiment3']
})
票数 2
EN

Stack Overflow用户

发布于 2016-07-26 18:47:05

我还没有运行过,如果失败了,很抱歉。如果你说它是一个列表,比如[ location,实验,位置,实验],那么:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38587083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档