Track name and list elements and give them default values extracted from a file: Python
Question: 
    Input file format is like below: 
    
     Label A
    {
      Zebra,
      Lion,
      Python,
      Cat,
      Dog,
      Blah,
    }
     Label B
    {
      India,
      Nigeria,
      Malaysia,
      Peru,
    }
    

I want to assign a default value to these elements, I was thinking I will form
a multi-dim list/array and then it would be easy to assign. Right now I have
the name and the elements in a variable.

    
    
    Code: 
    
    from numpy import *
    
    for match in re.finditer(r'Label (\w+)\s+([^\n]+)\s*\{(.*?)\}', input_file_read, re.DOTALL):
        Group_name = match.group(1)
        Group_elements = str(match.group(3).strip())
        ele_names = Group_pins.split(',')
        ele_list = [ele.strip() for ele in ele_names]
        ele_array.extend(pins_list)
    print ele_array
    
    To the above code I also tried: 
    
    ele_matrix = np.array(ele_array)
    
    Output: 
    ele_array = ['Zebra','Lion','Python','Cat','Dog','Blah','India','Nigeria','Malaysia','Peru']
    

I want:

    
    
    ([A, Zebra, Default_value]
    [A, Lion, Default_value]
    [A, Python, Default_value]
    [A, Cat, Default_value]
    [A, Dog, Default_value]
    [A, Blah, Default_value]
    [B, India, Default_value]
    [B, Nigeria, Default_value]
    [B, Malaysia, Default_value]
    [B, Peru, Default_value])
    

It should be a **single matrix** , with **n*3** where **n** is the **total
elements** in both the groups.


Answer: This should do it

    
    
    import re
    
    # Reading File Data
    handle = open("input.txt", "r")
    input_file_read = "".join(handle.readlines())
    handle.close()
    # print input_file_read
    _PATTERN_ = re.compile('^[\s]*Label[\s]*(\w)+[\s\n\{]*([^\}]+)', re.MULTILINE);
    _DEFAULT_VALUE = 1
    _Myarray = []
    for match in _PATTERN_.finditer(input_file_read):
        Group_name = match.group(1).strip()
        # print Group_name
        elements = match.group(2).replace(',', '').split()
        # print elements
        # _Myarray.append([Group_name, i, _DEFAULT_VALUE] for i in elements)
        for i in elements:
            _Myarray.append([Group_name, i, _DEFAULT_VALUE])
    
    print _Myarray
    

