我有一个创建为
的多个列表[['Mappings_1:', 'AMA111:AMA112,EM5', 'EM3,EM4',
'EM1,EM2'],
['Mappings_2:', 'AMA111:AMA112,EM5', 'EM3,EM4'],
['Mappings_3:', 'AMA111:AMA112,EM5', 'EM3,EM4',
'EM1,EM2', 'FPA1,FPA1', 'FPA3:FPA4,AMA113']]
我有一个可以保存值的选择参数' Mappings_1'或者' Mappings_2'或者' Mappings_3',
如果值为' Mappings_1'然后我想通过分配Id将['Mappings_1:', 'AMA111:AMA112,EM5', 'EM3,EM4', 'EM1,EM2']
分开到单独的列表中,如下所示
valid_map = [{'Id': '1', 'mapping': 'AMA111:AMA112,EM5'},
{'Id': '2', 'mapping': 'EM3,EM4'},
{'Id': '3', 'mapping': 'EM1,EM2'}]
根据所选参数,我需要通过分配通道ID来分隔特定列表。
请分享一些想法
答案 0 :(得分:1)
这非常简单 - 跳过列表中的每个子列表,检查哪一个与您的选择参数具有相同的第一个元素(例如Mappings_1
),然后转换 rest 将该子列表放入词典列表中。
def list_to_mapping(lst, selection):
for sublst in lst: # step over sublists
if sublst[0] == selection: # if selection parameter matched
# build list of dictionaries
valid_map = [{'id': index+1, 'mapping': value} # index+1 for 1-based indexing
for index, value in enumerate(sublst[1:])]
return valid_map
lst = [['Mappings_1:', 'AMA111:AMA112,EM5', 'EM3,EM4', 'EM1,EM2'],
['Mappings_2:', 'AMA111:AMA112,EM5', 'EM3,EM4'],
['Mappings_3:', 'AMA111:AMA112,EM5', 'EM3,EM4', 'EM1,EM2', 'FPA1,FPA1', 'FPA3:FPA4,AMA113']]
result = list_to_mapping(lst, 'Mappings_1:')
print(result)
结果(格式化):
[{'mapping': 'AMA111:AMA112,EM5', 'id': 1},
{'mapping': 'EM3,EM4', 'id': 2},
{'mapping': 'EM1,EM2', 'id': 3}]
答案 1 :(得分:1)
您是否可以控制原始列表生成?也许你应该使用字典。
类似的东西:
valid_maps = {'Mappings_1': ['AMA111:AMA112,EM5', 'EM3,EM4', 'EM1,EM2'],
'Mappings_2': ['AMA111:AMA112,EM5', 'EM3,EM4'],
'Mappings_3': ['AMA111:AMA112,EM5', 'EM3,EM4','EM1,EM2',
'FPA1,FPA1', 'FPA3:FPA4,AMA113']}
我使用列表作为字典中的值,因为' id'标签似乎不会增加太多。
答案 2 :(得分:0)
我认为您的多个列表效率不高。你应该使用dict。但是你可以试试这样的东西。
#!/usr/bin/env python
#-*-coding:utf-8-*-
multiple_list = [
['Mappings_1', 'AMA111:AMA112,EM5', 'EM3,EM4','EM1,EM2'],
['Mappings_2', 'AMA111:AMA112,EM5', 'EM3,EM4'],
['Mappings_3', 'AMA111:AMA112,EM5', 'EM3,EM4','EM1,EM2', 'FPA1,FPA1', 'FPA3:FPA4,AMA113']
]
valid_map = []
selection = "Mappings_1"
i = 0
while((i < len(multiple_list)) and (multiple_list[i][0]!= selection)):
print multiple_list[i][0]
i += 1
if(i<len(multiple_list)):
j = 1
while(j<len(multiple_list[i])):
tmpDict = dict()
tmpDict['id'] = j
tmpDict['mapping'] = multiple_list[i][j]
valid_map.append(tmpDict)
j += 1
print valid_map
但我的建议改变了你的数据模型。
#!/usr/bin/env python
#-*-coding:utf-8-*-
multiple_list = {
'Mappings_1' : ['AMA111:AMA112,EM5', 'EM3,EM4','EM1,EM2'],
'Mappings_2' : ['AMA111:AMA112,EM5', 'EM3,EM4'],
'Mappings_3' : ['AMA111:AMA112,EM5', 'EM3,EM4','EM1,EM2', 'FPA1,FPA1', 'FPA3:FPA4,AMA113']
}
valid_map = []
selection = 'Mappings_1'
if(selection in multiple_list):
for i in xrange(0,len(multiple_list[selection])):
tmpDict = {}
tmpDict["id"] = i + 1
tmpDict["mapping"] = multiple_list[selection][i]
valid_map.append(tmpDict)
i += 1
print valid_map
答案 3 :(得分:0)
In [1]: data = [['Mappings_1:', 'AMA111:AMA112,EM5', 'EM3,EM4', 'EM1,EM2'], ['Mappings_2:', 'AMA111:AMA112,EM5', 'EM3,EM4'], ['Mappings_3:', 'AMA111:AM
A112,EM5', 'EM3,EM4', 'EM1,EM2', 'FPA1,FPA1', 'FPA3:FPA4,AMA113']]
In [2]: result = {x[0]:[{"id": y[0], "mapping":y[1]} for y in list(enumerate(x))[1:]] for x in data}
In [3]: result["Mappings_1:"]
Out[3]:
[{'id': 1, 'mapping': 'AMA111:AMA112,EM5'},
{'id': 2, 'mapping': 'EM3,EM4'},
{'id': 3, 'mapping': 'EM1,EM2'}]