读书人

怎么在for循环外 调用for循环里面的数

发布时间: 2012-06-15 19:37:05 作者: rapoo

如何在for循环外 调用for循环里面的数据

Python code
#!/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








不过最后5行 想把key相同元素放到一起 没效果

结果是这样,第三、第四行key相同
{'192.168.90.15.1750 > 58.58.58.58.8000:': '0:10:30'}
{'192.168.90.20.1386 > 58.58.58.58.81:': '0:09:01'}
{'192.168.90.119.1891 > 220.170.79.229.80:': '0:00:00'}
{'192.168.90.119.1891 > 220.170.79.229.80:': '0:00:03'}
{'192.168.90.118.3594 > 58.58.58.58.1604:': '0:08:00'}


原始数据如下:
2012-06-11 08:28:27 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:28:27 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:28:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:06 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:29:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:29:22 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:29:22 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:29:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:17 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:30:17 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:30:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:51 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:31:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:31:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:31:12 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:31:12 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:31:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:31:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:32:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:32:06 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:32:07 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]


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

读书人网 >perl python

热点推荐