0

I have a multiple list created as

[['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']]

I have one selection parameter that can hold a value as 'Mappings_1' or 'Mappings_2' or 'Mappings_3',

If value is 'Mappings_1' then I want to separate the ['Mappings_1:', 'AMA111:AMA112,EM5', 'EM3,EM4', 'EM1,EM2'] in a separate list by assign Id as shown below

valid_map = [{'Id': '1', 'mapping': 'AMA111:AMA112,EM5'},
             {'Id': '2', 'mapping': 'EM3,EM4'},
             {'Id': '3', 'mapping': 'EM1,EM2'}]

Based on selected parameter, I need to separate the particular list by assign channel id.

Please share some idea

1
  • Post the code you have so far - I think it will help us understand what you're trying to do. Commented Jun 12, 2014 at 7:05

4 Answers 4

1

This is pretty straightforward - step over each sublist in your list, check which one has the same first element as your selection parameter (e.g. Mappings_1), and then convert the rest of that sublist into a list of dictionaries.

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)

Result (formatted):

[{'mapping': 'AMA111:AMA112,EM5', 'id': 1}, 
 {'mapping': 'EM3,EM4', 'id': 2}, 
 {'mapping': 'EM1,EM2', 'id': 3}]
Sign up to request clarification or add additional context in comments.

Comments

1

Do you have control over the original list generation? Maybe you should rather use a dictionary.

Something like:

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']}

I used lists as the values in the dictionary, since the 'id' tags don't seem to add much.

Comments

0

I think your multiple list is not efficient. You should use dict. But you can try something like that.

#!/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

But my suggestion change your data model.

#!/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

Comments

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'}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.