简体   繁体   English

Python词典:将多个值列表合并为一个唯一值列表

[英]Python dictionaries: merging multiple value lists into a single list of unique values

I am just learning Python using Python 2.7. 我只是使用Python 2.7学习Python。 I have a csv file with two columns. 我有一个包含两列的csv文件。 The columns are: 列是:

Coll_id: the entries may be single collectors or may be groups Coll_id:条目可以是单个收集者,也可以是组

Participant_Coll_id: if the Coll_id is a single collector then the value will be null. Participant_Coll_id:如果Coll_id是单个收集器,则该值将为null。 If the Coll_id is a group then there will be a single row for each Participant in the group. 如果Coll_id是一个组,那么该组中的每个参与者将只有一行。

A sample is here: 样本在这里:

Coll_id,Participant_Coll_id<br>
ARA,ARG  
ARA,RAT  
ARG,NULL  
BRSAR,SGMB  
BRSAR,SANTM  
BRSAR,CRSR  
BRSAR,RAT  
CRSR,NULL  
DBY,NULL  
HZIE,NULL  
RAT,NULL  
SANTM,NULL  
SGMB,NULL  
ARG,NULL  
DRS,CRSR  
DRS,RAT  
DRS,ARG  

For each collector (coll_id), I'm trying to create a list of all the other collectors they have collected with. 对于每个收集器(coll_id),我正在尝试创建他们收集的所有其他收集器的列表。 I have tried to pull together code to do the following and it is so close now: 我试图将代码拉到一起来执行以下操作,它现在非常接近:

#This is giving me a dictionary with each COLL_ID having a list of PARTICIPANT_COLL_IDs

with open('colls_mv1.csv', 'r') as f:
    reader = csv.DictReader(f, ['COLL_ID', 'PARTICIPANT_COLL_ID'])
    data1 = defaultdict(list)

    for line in reader:
        data1[line['COLL_ID']].append(line['PARTICIPANT_COLL_ID'])


#And this is giving me a dictionary with each PARTICIPANT_COLL_ID having a list of COLL_IDs
with open('colls_mv1.csv', 'r') as f:
    reader = csv.DictReader(f, ['COLL_ID', 'PARTICIPANT_COLL_ID'])
    data2 = defaultdict(list)

    for line in reader:
        if line['PARTICIPANT_COLL_ID'] != 'NULL':
            data2[line['PARTICIPANT_COLL_ID']].append(line['COLL_ID'])

dict3 = {k: [data1[i] for i in v] for k, v in data2.items()}

print dict3

I'm getting the following output: 我得到以下输出:

{'SGMB': [['SGMB', 'SANTM', 'CRSR', 'RAT']], 'CRSR': [['SGMB', 'SANTM', 'CRSR', 'RAT'], ['CRSR', 'RAT', 'ARG']], 'RAT': [['ARG', 'RAT'], ['SGMB', 'SANTM', 'CRSR', 'RAT'], ['CRSR', 'RAT', 'ARG']], 'PARTICIPANT_COLL_ID': [['PARTICIPANT_COLL_ID']], 'ARG': [['ARG', 'RAT'], ['CRSR', 'RAT', 'ARG']], 'SANTM': [['SGMB', 'SANTM', 'CRSR', 'RAT']]}

What I would like is to merge the value lists together for each key, to remove duplicates and to remove the key from the value list: 我想要的是将值列表合并为每个键,删除重复项并从值列表中删除键:

{'SGMB': ['SANTM', 'CRSR', 'RAT'], 'CRSR': ['SGMB', 'SANTM', 'RAT', 'ARG'], 'RAT': ['ARG', 'SGMB', 'SANTM', 'CRSR'], 'PARTICIPANT_COLL_ID': [['PARTICIPANT_COLL_ID']], 'ARG': ['RAT', 'CRSR'], 'SANTM': ['SGMB', 'CRSR', 'RAT']}

Iterate over the lists, remove the keys, and deduplicate 迭代列表,删除密钥和重复数据删除

>>> res = {'SGMB': [['SGMB', 'SANTM', 'CRSR', 'RAT']], 'CRSR': [['SGMB', 'SANTM', 'CRSR', 'RAT'], ['CRSR', 'RAT', 'ARG']], 'RAT': [['ARG', 'RAT'], ['SGMB', 'SANTM', 'CRSR', 'RAT'], ['CRSR', 'RAT', 'ARG']], 'PARTICIPANT_COLL_ID': [['PARTICIPANT_COLL_ID']], 'ARG': [['ARG', 'RAT'], ['CRSR', 'RAT', 'ARG']], 'SANTM': [['SGMB', 'SANTM', 'CRSR', 'RAT']]}
>>> newres = {k: list({x for t in v for x in t if x != k}) for k, v in res.iteritems()}
>>> newres
{'SGMB': ['CRSR', 'SANTM', 'RAT'], 'CRSR': ['SANTM', 'SGMB', 'RAT', 'ARG'], 'RAT': ['CRSR', 'SANTM', 'SGMB', 'ARG'], 'PARTICIPANT_COLL_ID': [], 'ARG': ['CRSR', 'RAT'], 'SANTM': ['CRSR', 'RAT', 'SGMB']}

demo : http://ideone.com/87HKM9 演示: http//ideone.com/87HKM9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM