To increment and loop till the end of file?
consider the following code:
history_begins = 1; history_ends = 5000
historyjobs = []; targetjobs = []
listsub = []; listrun = [];
def check(inputfile):
f = open(inputfile,'r')
lines = f.readlines()
for line in lines:
job = line.split()
if( int(job[0]) < history_ends ):
historyjobs.append(job) #appends all lines to the
historyjobs list from inputfile whose col1 value is < 5000
else:
targetjobs.append(job) #appends all lines to the
historyjobs list from inputfile whose col1 value is > 5000
print targetjobs[0] #To print the first list in targetjobs list
j = 0
for i in range(len(historyjobs)):
if( (int(historyjobs[i][3]) == int(targetjobs[j][3])) and
(int(historyjobs[i][4]) == int(targetjobs[j][4])) and
(int(historyjobs[i][5]) == int(targetjobs[j][5])) ): #comparing
the item 3,4,5 of all the lists in historyjobs list with the
item 3,4,5 of the first list in targetjobs list
listsub.append(historyjobs[i][1])
listrun.append(historyjobs[i][2])
print listsub
print len(listsub)
print listrun
def main():
check('newfileinput')
if __name__ == '__main__':
main()
The result i obtained was:
['5000', '1710390', '930', '8', '9', '2']
['767220', '769287', '770167', '770276', '770791', '770835', '771926',
'1196500', '1199789', '1201485', '1206331', '1206467', '1210929',
'1213184', '1213204', '1213221', '1361867', '1361921', '1361949',
'1364886', '1367224', '1368005', '1368456', '1368982', '1369000',
'1370365', '1370434', '1370551', '1371492', '1471407', '1709408',
'1710264', '1710308', '1710322', '1710350', '1710365', '1710375']
37
['2717', '184', '188', '163', '476', '715', '1099', '716', '586',
'222', '456', '457', '582', '418', '424', '425', '177', '458', '236',
'2501', '3625', '1526', '299', '1615', '1632', '1002', '379', '3626',
'1003', '1004', '3625', '1002', '1019', '1037', '1066', '998', '977']
Now what i need to do is to increment the history_ends value i,e
history_ends = 5000 initial assignment and prints the results
next history_ends = 5001
next history_ends = 5002-------------------till the end of the input file
so that the result i need is:
['5000', '1710390', '930', '8', '9', '2'] #when history_ends=5000
['767220', '769287', '770167', '770276', '770791', '770835', '771926',
'1196500', '1199789', '1201485', '1206331', '1206467', '1210929',
'1213184', '1213204', '1213221', '1361867', '1361921', '1361949',
'1364886', '1367224', '1368005', '1368456', '1368982', '1369000',
'1370365', '1370434', '1370551', '1371492', '1471407', '1709408',
'1710264', '1710308', '1710322', '1710350', '1710365', '1710375']
37
['2717', '184', '188', '163', '476', '715', '1099', '716', '586', '222',
'456', '457', '582', '418', '424', '425', '177', '458', '236', '2501',
'3625', '1526', '299', '1615', '1632', '1002', '379', '3626', '1003',
'1004', '3625', '1002', '1019', '1037', '1066', '998', '977']
['5001', '1710554', '4348', '8', '9', '2'] #when history_ends=5001
['767220', '769287', '770167', '770276', '770791', '770835', '771926',
'1196500', '1199789', '1201485', '1206331', '1206467', '1210929',
'1213184', '1213204', '1213221', '1361867', '1361921', '1361949',
'1364886', '1367224', '1368005', '1368456', '1368982', '1369000',
'1370365', '1370434', '1370551', '1371492', '1471407', '1709408',
'1710264', '1710308', '1710322', '1710350', '1710365', '1710375',
'1710390']
38
['2717', '184', '188', '163', '476', '715', '1099', '716', '586', '222',
'456', '457', '582', '418', '424', '425', '177', '458', '236', '2501',
'3625', '1526', '299', '1615', '1632', '1002', '379', '3626', '1003',
'1004', '3625', '1002', '1019', '1037', '1066', '998', '977', '930']
['5002', '1710791', '18047', '32', '137', '3'] #when history_ends=5002
['1302665', '1364654', '1385017']
3
['17285', '61465', '17961']
''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
Till the end of the file
can any one suggest how i can modify my code to be able to do this in
python.It would be helpful............
No comments:
Post a Comment