1
안녕 아마 목록의 목록을 작성파이썬 -
뭔가를 분명 실종 모든 목록의 목록을 분류 (C++에서 표기 "벡터 \\>"), 다음 ("기록")를 사용하여 내부 목록을 정렬 시도 일부 필드는 정렬 키로 사용됩니다. 그리고 그것은 효과가 없을 것입니다. "lambda"와 "itemgetter"를 사용하여 두 가지 버전을 시도했습니다. 오류나 경고가 없습니다. 내가 도대체 뭘 잘못하고있는 겁니까? //
* * 내 코드 :
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<list<field>>
#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)) #----> does not work
sorted(self.mx, key=lambda fields: fields[5]) #--------> 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))
** * ** * ** *
:클래스 fwReport 시작 ()
DEF 주 :
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")
0 [ 'B', 'A', 'C'] 'B', 'A', 'C']
1 'A' [ 'a', 'c', 'b'] [ 'a', 'c', 'c'] [ 'a', 'b' B ']
****
작업 인터넷 네.
@Ned BATCHELDER - 감사
doh ... 감사합니다! – Nikiton
여전히 in-place 정렬 할 수 있습니다 :'self.mx.sort (key = ...)' – 9000
@ 9000 감사합니다. 현재와 정렬 된 (..)이 모두 작동 중입니다. 아마 정신 장애가 있었을거야. 다음 번엔 ... – Nikiton