0

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 :)

2
  • 1
    Welcome to Stack Overflow. Please edit your question to show code as it is in your script (you can copy/paste from your editor, highlight it all and press ctrl+k). Also, the purpose of your question is not clear; what exactly is wrong with your approach? Commented Aug 14, 2016 at 19:38
  • 1
    What do you expect for s in range(i, i) to do? There is no range of integers between two equal integers. Also, you don't reference s in the loop itself, and info is not defined? Commented Aug 14, 2016 at 19:41

1 Answer 1

2

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', '
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.