Python: nested list output
Question: I am reading data from a text file like this

    
    
    >VAL1;Date1;K123 K135;A lot of text  
    >VAL2;Date2;K231 K389;more text  
    >VAL3;Date3;K123;even more text  
    >VAL4;Date4;K389;even more text  
    >VAL5;Date5;K634 K123 K888;even more text  
    

Desired output while col4 is a count of col2 and col5 is a count of col1

    
    
    >K123;VAL1;Date1;2;3  
    >K135;VAL1;Date1;1;2  
    >K231;VAL2;Date2;1;2  
    >K398;VAL2;Date2;2;2  
    >K123;VAL3;Date3;2;3  
    >K398;VAL4;Date4;2;1  
    >K634;VAL5;Date5;3;1  
    >K123;VAL5;Date5;3;3  
    >K888;VAL5;Date5;3;1  
    

Idea was to read it into lists and nested lists like this.

    
    
    List = [['VAL1','Date1',['K123','K125'],'A lot of text'],['VAL2','Date2',['K231','K389'],'more     text'],...]  
    

This is my code so far to create list and nested lists but I can´t make it to
the desired output. Can you support?

    
    
     import re
    
        raw_data = open('C:\Users\denis.gerhardt\DMS\INC.txt').read().strip('\n')
        val = re.findall('INC.+',raw_data)
        meta=[]
        for item in val:
        meta.append(item.split(';'))
        k=[]
        for k in meta:
            k.append(re.findall(r'\bK[0-9]+',k[2]))
        print meta
        close(raw_data)
    


Answer: Clever is good, but in this case, you should break the problem apart into
comprehensible bits and then put it all back together. Not only will this be
easier, it will also be more readable and more maintainable long term.

For instance, one of your columns is a count of your K values. There are many
ways of doing this:

    
    
    # ['VAL1;Date1;K123 K135;A lot of text', ... ]
    val = """VAL1;Date1;K123 K135;A lot of text
    VAL2;Date2;K231 K389;more text
    VAL3;Date3;K123;even more text
    VAL4;Date4;K389;even more text
    VAL5;Date5;K634 K123 K888;even more text""".split('\n')
    
    # Most straightforward method:
    # Parse the lines then use a dictionary to keep count
    count_k_1 = {}
    for line in val:
        array = line.split(';')
        ks = array[2].split(' ')
        for k in ks:
            try:
                count_k_1[k] += 1
            except KeyError:
                count_k_1[k] = 1
    print count_k_1
    
    # Fancy method:
    # Use a collection.Counter and then parse in a list comprehension
    import collections
    count_k_2 = collections.Counter([item for line in val for item in
            line.split(';')[2].split(' ')])
    print count_k_2
    

Output:

    
    
    {'K888': 1, 'K231': 1, 'K123': 3, 'K634': 1, 'K389': 2, 'K135': 1}
    Counter({'K123': 3, 'K389': 2, 'K888': 1, 'K231': 1, 'K634': 1, 'K135': 1})
    

There are probably two more major things to write:

  1. How to count the values.
  2. How to print the pairs of keys and values so that your final output has 9 lines, not 5.

These are left as an exercise for the reader.

