-3

Recently I was looking at some code related to a deep learning paper, repo here: https://github.com/wuyifan18/DeepLog/blob/master/LogKeyModel_predict.py

This has more to do with python so I will not mention anything else about it. Below is a file we need to parse:

5 5 5 22 11 9 11 9 11 9 26 26 26 23 23 23 21 21 21 
5 5 22 5 11 9 11 9 11 9 26 26 26 
5 22 5 5 11 9 11 9 11 9 26 26 26 
5 22 5 5 11 9 11 9 11 9 26 26 26 
5 22 5 5 11 9 11 9 11 9 26 26 26 23 23 23 21 21 21 
22 5 5 5 11 9 11 9 11 9 26 26 26 23 23 23 21 21 21 
5 22 5 5 11 9 11 9 11 9 26 26 26 23 23 23 21 21 21 
5 5 5 22 11 9 11 9 11 9 26 26 26 2 23 23 23 21 21 21 
5 22 5 5 11 9 11 9 11 9 26 26 26

The following function is supposed to parse it. First, we take each line, and make a list with the elements being the numbers in that line separated by spaces. Then we subtract said numbers by one at line ###. What happens next?

def generate(name):
    hdfs = set()
    # hdfs = []
    with open('data/' + name, 'r') as f:
        for ln in f.readlines():
            ln = list(map(lambda n: n - 1, map(int, ln.strip().split()))) ###
            ln = ln + [-1] * (window_size + 1 - len(ln))
            # print(ln)
            hdfs.add(tuple(ln))
    print('Number of sessions({}): {}'.format(name, len(hdfs)))
    return hdfs

I am not sure what the purpose of ln = ln + [-1] * (window_size + 1 - len(ln)) is exactly. What is it doing? I have not seen list multiplication being used in many places before, so I am not sure. When I try and print out more of it, it seems that -1 is not present in ln at all. Anyone have some idea?

herophant
  • 642
  • 7
  • 16
  • 2
    Multiplication of a list simply creates a list with n times the element (be careful with mutables like lists and classes, this can be surprising, try it). So this pads the line with some - 1 values, based on window size and line length. – Jan Christoph Terasa Jan 30 '20 at 05:12
  • Does this answer your question? [Create an empty list in python with certain size](https://stackoverflow.com/questions/10712002/create-an-empty-list-in-python-with-certain-size), [Python append() vs. + operator on lists, why do these give different results?](https://stackoverflow.com/questions/2022031/python-append-vs-operator-on-lists-why-do-these-give-different-results) – metatoaster Jan 30 '20 at 05:20

2 Answers2

0

Without delving into the code, the idea is to make all the lines the same length, based on the window

ShpielMeister
  • 1,417
  • 10
  • 23
0

If your window size is 10, and a line contains only 5 entries, your list will look like: [1, 2, 3, 4, 5, -1, -1, -1, -1, -1], this is to deal with the static sized window.