James
James

Reputation: 71

How to read specific multiple lines using for loop at the same time?

contents are in list[info] from a text file:

1,1,1703,385,157,339,1,-1,-1,-1

1,3,1293,455,83,213,1,-1,-1,-1

1,4,259,449,101,261,1,-1,-1,-1

1,5,1253,529,55,127,1,-1,-1,-1

2,1,1699,383,159,341,1,-1,-1,-1

2,3,1293,455,83,213,1,-1,-1,-1

2,4,261,447,101,263,1,-1,-1,-1

2,5,1253,529,55,127,1,-1,-1,-1

3,1,1697,383,159,343,1,-1,-1,-1

3,3,1293,455,83,213,1,-1,-1,-1

3,4,263,447,101,263,1,-1,-1,-1

3,5,1255,529,55,127,1,-1,-1,-1

4,1,1695,383,159,343,1,-1,-1,-1

4,3,1293,455,83,213,1,-1,-1,-1

4,4,265,447,101,263,1,-1,-1,-1

4,5,1257,529,55,127,1,-1,-1,-1

.
.
.

they consist of like these

I am going to show some pictures. so I think that you don't have to care about image_list and files. anyway, I want to read like these:

conclusion = if info[0] is 1, i want to read info[2], info[3], info[4] info[5] of lines that they starts as info[0] is 1.

in other words,

if info[0] is 1, I want to print like below

1703,385,157,339

1293,455,83,213

259,449,101,261

1253,529,55,127 

at the same time

my code is below:

**marks = [int(info[0])]

      for i, images_files in zip(marks, image_list):

             for s in range(i, i):

                  print int(info[2]), int(info[3]), int(info[4]), int(info[5])**

please help me :)

Upvotes: 0

Views: 64

Answers (1)

Karin
Karin

Reputation: 8600

You can create a dictionary of integer value to list of those four values you want to print:

from collections import defaultdict

lines = [
    '1,1,1703,385,157,339,1,-1,-1,-1',
    '1,3,1293,455,83,213,1,-1,-1,-1',
    '1,4,259,449,101,261,1,-1,-1,-1',
    '1,5,1253,529,55,127,1,-1,-1,-1',
    '2,1,1699,383,159,341,1,-1,-1,-1',
    '2,3,1293,455,83,213,1,-1,-1,-1',
    '2,4,261,447,101,263,1,-1,-1,-1',
    '2,5,1253,529,55,127,1,-1,-1,-1',
    '3,1,1697,383,159,343,1,-1,-1,-1',
    '3,3,1293,455,83,213,1,-1,-1,-1',
    '3,4,263,447,101,263,1,-1,-1,-1',
    '3,5,1255,529,55,127,1,-1,-1,-1',
    '4,1,1695,383,159,343,1,-1,-1,-1',
    '4,3,1293,455,83,213,1,-1,-1,-1',
    '4,4,265,447,101,263,1,-1,-1,-1',
    '4,5,1257,529,55,127,1,-1,-1,-1',
]

line_map = defaultdict(list)
for line in lines:
    values = line.split(',')
    line_map[int(values[0])].append(','.join(values[2:6]))

print line_map[1]  # ['1703,385,157,339', '1293,455,83,213', '259,449,101,261', '

Upvotes: 2

Related Questions