Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - sorted list of lists
    primarykey
    data
    text
    <p>Hi all I am probably missing something obvious</p> <p>Creating list of lists (in C++ notation "vector\\>"), then trying to sort inner lists ("records") using some of the fields as a sort keys. And it would not work. Tryed two different version: with "lambda" and with "itemgetter". There is no error or warning. What am I doing wrong?</p> <p>//<strong><em>*</em>*</strong> my code: start</p> <p>class fwReport:</p> <pre><code>def __init__(self): #each field from firewall log file, 17 all together self.fieldnames = ("date", "time", "action", "protocol", \ "src-ip", "dst-ip", "src-port", "dst-port" \ "size", "tcpflags", "tcpsyn", "tcpack", \ "tcpwin", "icmptype", "icmpcode", "info", "path") self._fields = {} self.mx = list() self.dst_ip = collections.Counter() self.src_ip = collections.Counter() def openfn(self): try: with open(fn) as f: data = f.read() except IOError as err: raise AssertionError("Can't open %s for reading: %s" % (fn, err)) return #make a matrix out of data, smth. like list&lt;list&lt;field&gt;&gt; #skip first 5 lines (file header) for fields in data.split("\n")[5:25]: temp = fields.split(" ")[:6] #take first 7 fields self.src_ip[temp[4]] += 1 #count source IP self.dst_ip[temp[5]] += 1 #count destination IP self.mx.append(temp) #build list of lists #sorted(self.mx, key=itemgetter(5)) #----&gt; does not work sorted(self.mx, key=lambda fields: fields[5]) #--------&gt; does not work for i in range(len(self.mx)): print(i, " ", self.mx[i][5]) #print(self.dst_ip.most_common(16)) #print(self.src_ip.most_common(16)) print(self.mx[:5][:]) #print(len(self.dst_ip)) </code></pre> <h1><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em></strong></h1> <p>def main():</p> <pre><code>mx = [["a", "b", "c"], ["a", "c", "b"], ["b", "a", "c"]] mx = sorted(mx, key=lambda v: v[1]) for i in range(len(mx)): print(i, " ", mx[i], " ", mx[i], end="\n") </code></pre> <p>0 ['b', 'a', 'c'] ['b', 'a', 'c']</p> <p>1 ['a', 'b', 'c'] ['a', 'b', 'c']</p> <p>2 ['a', 'c', 'b'] ['a', 'c', 'b']</p> <h1><em><strong></em>****</strong></h1> <p>Working fine.</p> <p>@Ned Batchelder - thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload