如何在for循环外 调用for循环里面的数据
#!/usr/bin/env pythonimport datetimeaDict = {}with open(r'D:\python_dev\1.txt') as fd: for line in fd: a = line.split() aday, atime, asrc, adst, aflag = a[0], a[1], a[2], a[3], a[5] aKey = asrc + ' > ' + adst if aKey in aDict: aDict[aKey].append([aday+" "+atime, aflag]) else: aDict[aKey] = [[aday+" "+atime, aflag]]delimiter = '[P.]'for key in aDict: arry = [] tmp = [] for x in aDict[key]: #print key,'x',x s = x[1] #print 's',s if s != delimiter: tmp.append(x[:1]) else: arry.append(tmp) tmp = [] arry.append(tmp) for x in arry: if len(x) < 2 : continue else: #print key for t1 in x[0]: timedict = {} for t2 in x[-1]: #print t1,t2 date1 = datetime.datetime.strptime(t1,"%Y-%m-%d %H:%M:%S") date2 = datetime.datetime.strptime(t2,"%Y-%m-%d %H:%M:%S") datesent = str(date2 -date1) #print datesent #print 'key',key if key in timedict: timedict[key].append(datesent) else: timedict[key] = datesent print timedict
2012-06-11 08:32:07 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:32:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:32:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:33:02 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:33:02 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:33:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:33:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:33:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:33:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:33:51 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:33:57 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:33:57 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:34:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:34:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:34:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:34:52 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:34:52 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:35:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:35:06 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:35:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:35:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:35:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:35:47 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:35:47 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:36:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:36:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:36:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:36:42 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:36:42 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:36:50 192.168.90.119.1891 220.170.79.229.80: Flags [S]
2012-06-11 08:36:50 192.168.90.119.1891 220.170.79.229.80: Flags [.]
2012-06-11 08:36:50 192.168.90.119.1891 220.170.79.229.80: Flags [P.]
2012-06-11 08:36:50 192.168.90.119.1891 220.170.79.229.80: Flags [.]
2012-06-11 08:36:50 192.168.90.119.1891 220.170.79.229.80: Flags [.]
2012-06-11 08:36:53 192.168.90.119.1891 220.170.79.229.80: Flags [F.]
2012-06-11 08:36:51 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:37:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:37:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:37:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:37:37 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:37:38 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:37:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:38:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:38:07 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:38:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:38:32 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:38:33 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:38:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:39:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:39:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
[解决办法]
timedict中,每个key对应一个list,list的元素都是string