I got data that looks like this(below), I would like to write a while loop using python that goes line by line, and as long as the word in the first column is not changing the scripts check the value of the third column and if the value of the third column is the minimum it gives back the value of the third column and the value of the second column of the same line.
ZYG11B 0000 1
ZYG11B 0100 2
ZYG11B 0001 3
ZYG11A 0001000000000000 1
ZYG11A 0011000000000000 2
ZYG11A 0000000000000000 3
ZYG11A 0011000001000000 4
ZYG11A 0001000100000000 5
ZYG11A 0011000001000100 6
ZYG11A 0001000001000000 7
ZYG11A 0001000001000100 8
ZYG11A 0000000100000000 9
ZYG11A 0011010000000000 10
ZYG11A 0001010000000000 11
ZYG11A 0011000000010000 12
ZYG11A 0000000000000001 13
ZYG11A 0001000000000001 14
ZYG11A 0000010000000000 15
ZYG11A 0001000000010000 16
ZYG11A 0000110000000000 17
ZYG11A 0101000000000000 18
ZYG11A 0100000000000000 19
ZYG11A 0011010001000000 20
ZYG11A 0011000011000000 21
ZYG11A 0011110000000000 22
ZYG11A 0011010001000100 23
ZYG11A 0011000011001100 24
ZYG11A 0011000011000100 25
ZYG11A 0001000101000100 26
ZYG11A 0001000000000100 27
ZYG11A 0000110000000001 28
ZYG11A 0000001100000000 29
If the Python 2.7 code below doesn't solve your problem, it should at least give you a starting point:
def ret_fields(file1):
prev_category = ''
min_field = 0
ret_list = []
line = file1.readline()
while (line):
[category,zero_one,field] = line.split()
if (prev_category == ''):
prev_category = category
min_field = field
if (category != prev_category):
min_field = field
if (int(field) <= int(min_field)):
ret_list.append([category,zero_one,field])
min_field = field
prev_category = category
line = file1.readline()
return ret_list
f = open("fields.txt")
try:
l = ret_fields(f)
for i in l:
print i
finally:
f.close()
With the "fields.txt" file as follows:
ZYG11B 0000 1
ZYG11B 0100 2
ZYG11B 0001 3
ZYG11B 1000 0
ZYG11A 0001000000000000 1
ZYG11A 0011000000000000 2
ZYG11A 0000000000000000 3
ZYG11A 0011000001000000 4
ZYG11A 0001000100000000 5
ZYG11A 0011000001000100 6
ZYG11A 0001000001000000 7
ZYG11A 0001000001000100 8
ZYG11A 0000000100000000 9
ZYG11A 0011010000000000 10
ZYG11A 0001010000000000 11
ZYG11A 0011000000010000 12
ZYG11A 0000000000000001 13
ZYG11A 0001000000000001 14
ZYG11A 0000010000000000 15
ZYG11A 0001000000010000 16
ZYG11A 0000110000000000 17
ZYG11A 0000110000000000 -1
ZYG11A 0101000000000000 18
ZYG11A 0100000000000000 19
ZYG11A 0011010001000000 20
ZYG11A 0011000011000000 21
ZYG11A 0011110000000000 22
ZYG11A 0011010001000100 23
ZYG11A 0011000011001100 24
ZYG11A 0011000011000100 25
ZYG11A 0001000101000100 26
ZYG11A 0001000000000100 27
ZYG11A 0000110000000001 28
ZYG11A 0000001100000000 29
ZYG11A 1001000000000000 0
The output is:
$ python min_fields.py
['ZYG11B', '0000', '1']
['ZYG11B', '1000', '0']
['ZYG11A', '0001000000000000', '1']
['ZYG11A', '0000110000000000', '-1']
$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.