【发布时间】:2017-10-08 12:21:25
【问题描述】:
我正在尝试定义一个函数来创建这样的字典:
d = {}
d['aod'] = [('nt', 'P3', ['af1', 'af2', 'af3']),
('t', 'P3', ['af1', 'af2', 'af3']),
('nv', 'P3', ['af1', 'af2', 'af3'])]
我可以得到这样的字典:
d = {'aod': ('nt', 'P3')}
使用下面的代码:
experiment = ['aod']
cond_peaks = ['nt_P3', 't_P3', 'nv_P3']
channel_lst = ['af1', 'af2', 'af3']
def create_dict(experiment, cond_peaks, channel_lst):
d = {}
for e in experiment:
for cp in cond_peaks:
cond = cp.split('_')[0]
peak = cp.split('_')[1]
cond_peak_tup = cond, peak
d[e] = cond_peak_tup
return d
由于元组是不可变的,我无法将 channel_lst 添加到字典值中。我的问题是如何创建一个字典,其中的值包含一个元组和一个列表?
【问题讨论】:
标签: python-3.x dictionary tuples