Search code examples
python-3.xcustom-lists

How to merge two lists at a delimited token in python3


I am a CS major at the University of Alabama, we have a project in our python class and I am stuck...probably for some stupid reason, but I cant seem to find the answer.

here is the link to the project, as it would be a pain to try and explain on here.

http://beastie.cs.ua.edu/cs150/projects/project1.html

here is my code:

import sys
from scanner import scan

def clInput():
    #Gets command line input

    log1 = sys.argv[1]
    log2 = sys.argv[2]
    name = sys.argv[3]

    if len(sys.argv) != 4:
        print('Incorrect number of arguments, should be 3')
        sys.exit(1)
    return log1,log2,name

def openFiles(log1,log2):
    #Opens sys.argv[1]&[2] for reading

    f1 = open(log1, 'r')
    f2 = open(log2, 'r')
    return f1, f2


def merge(log1,log2):
    #Merges parsed logs into list without '---'

    log1Parse = [[]]
    log2Parse = [[]]
    log1Count = 0
    log2Count = 0
    for i in log1:
        if i != ['---']:
            log1Parse[log1Count].append(i)
        else:
            log1Count += 1
            log1Parse.append([])

    for i in log2:
        if i != ['---']:
            log2Parse[log2Count].append(i)
        else:
            log2Count += 1
            log2Parse.append([])

return(log1Parse[0] + log2Parse[0] + log1Parse[1] + log2Parse[1])


def searchMerge(name,merged):
    #Searches Merged list for sys.argv[3]

    for i in range(len(merged)):
        if (merged[i][1] == name):
            print(merged[i][0],merged[i][1]," ".join(merged[i][2:]))



def main():
    log1,log2,name = clInput()
    f1,f2 = openFiles(log1,log2)

    #Sets the contents of the two scanned files to variables
    tokens1 = scan(f1)
    tokens2 = scan(f2)

    #Call to merge and search
    merged = merge(tokens1,tokens2)
    searchMerge(name,merged)




main()

ok. so heres the problem. We are to merge two lists together into a sorted master list, delimited at the ---'s

my two log files match the ones posted on the website i linked to above. This code works, however if there are more than two instances of the ---'s in each list, it will not jump to the next list to get the other tokens, and so forth. I have it working for two with the merge function. at the end of that function i return

return(log1Parse[0] + log2Parse[0] + log1Parse[1] + log2Parse[1])

but this only works for two instances of ---. Is there anyway i can change my return to look at all of the indexes instead of having to manually put in [0],[1],[2], etc.? I need it to delimit and merge for an arbitrary amount. Please help!!

p.s. disregard the noobness...im a novice, we all gotta start somewhere

p.p.s. - the from scanner import scan is a scanner i wrote to take in all of the tokens in a given list


Solution

  • so.py:

    import sys
    
    def main():
        # check and load command line arguments
        # your code
        if len(sys.argv) != 4:
            print('Incorrect number of arguments, should be 3')
            sys.exit(1)
    
        # open files using file io
        # your code
        f1 = open(log1, 'r')
        f2 = open(log2, 'r')
    
        # list comprehension to process and filter log files
        l1 = [ x.strip().split(" ",2) for x in f1.readlines() if x.strip() != "---" ]
        l2 = [ x.strip().split(" ",2) for x in f2.readlines() if x.strip() != "---" ]
    
        f1.close()
        f2.close()
    
        sorted_merged_lists = sorted(l1 + l2)
    
        results = [ x for x in sorted_merged_lists if x[1] == name ]
    
        for result in results:
            print result
    
    main()
    

    CLI:

    $ python so.py log1.txt log2.txt Matt
    ['12:06:12', 'Matt', 'Logged In']
    ['13:30:07', 'Matt', 'Opened Terminal']
    ['15:02:00', 'Matt', 'Opened Evolution']
    ['15:31:16', 'Matt', 'Logged Out']
    

    docs:

    http://docs.python.org/release/3.0.1/tutorial/datastructures.html#list-comprehensions
    http://docs.python.org/release/3.0.1/library/stdtypes.html?highlight=strip#str.strip
    http://docs.python.org/release/3.0.1/library/stdtypes.html?highlight=split#str.split
    http://docs.python.org/release/3.0.1/library/functions.html?highlight=sorted#sorted