我有一个python字典,我想将value [0]的值和值[1]的列表连接起来
如此
dict={ 'Key1':['MASTER',['_A','_B','_C']]}
并且调用Key1的预期输出是
[['MASTER_A','MASTER_B','MASTER_C']]
答案 0 :(得分:2)
使用嵌套理解:
d = {'Key1':['MASTER',['_A','_B','_C']]}
result_dict = {k: [v[0] + l for l in v[1]] for k,v in d.items()}
例如:
>>> result_dict = {k: [v[0] + l for l in v[1]] for k,v in d.items()}
>>> result_dict
{'Key1': ['MASTER_A', 'MASTER_B', 'MASTER_C']}
答案 1 :(得分:1)
尝试一下:
d = {'Key1':['MASTER',['_A','_B','_C']]}
out = [d['Key1'][0]+i for i in d['Key1'][1]]
输出:
['MASTER_A', 'MASTER_B', 'MASTER_C']
要将其分配给密钥,请执行:
d['Key1'] = out