Python Forum
Need print out lines before and after the match
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need print out lines before and after the match
#7
It looks simple because it is simple. You are doing this:
Quote:Find the letter d and return d, the letter before and the letter after.
0 a
1 b
[2 c
3 d
4 e]
5 f
6 g
Answer: [2:5]
That is simple.

When you are programming, how do you start? Do you write a detailed description of the problem you are trying to solve? I do. Even for simple programs like this. Writing down the problem description places it clearly in my mind. If I don't write down the problem description I may end up solving the wrong problem.

The next thing I do is draw pictures of the problem and my solution. Everyone is a visual thinker and everyone will benefit from drawing. I always write the program on paper first. I fill notebooks with problem descriptions and notes and drawings and pseudocode. If you had drawn a picture of your solution with a short list and maybe some index values I bet you wouldn't have had any trouble writing the code. You are trying to do you designing with Python. You are not comfortable with Python and that makes it a terrible tool for doing design.

My simple drawing above makes me realize there is a bug in my solution. Looking at it now I see the end of the slice was off. If you want the four before and the four after the slice should be lines[max(0,index-4):min(index+5, len(lines))] If index was 4 we would want lines 0 through 8. To get those lines the slice should be 0 to 9 where line[9] is not part of the slice.

max(index+5, len(lines)) does the same thing you were doing with max(0,index-4) except for the end. Thinking some more about it I don't think the min is required. If you ask for a slice that extends beyond the source, the slice is "sliced" to fit.
x = [0, 1, 2]
print(x[0:10])
Output:
[1, 2, 3]
The same is not true if you specify a negative value for the start of slice.
print(x[-2, 3])
Output:
[2, 3]
The same as x[3-2, 3]
Reply


Messages In This Thread
RE: Need print out lines before and after the match - by deanhystad - Aug-11-2020, 02:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Print the next 3 lines knob 3 383 May-22-2024, 12:26 PM
Last Post: andraee
  failing to print not matched lines from second file tester_V 14 6,538 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  If match not found print last line tester_V 2 3,020 Apr-26-2021, 05:18 AM
Last Post: tester_V
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,697 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,367 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Look for match in two files and print out in the first file Batistuta 0 1,679 Mar-03-2020, 02:27 PM
Last Post: Batistuta
  print python json dump onto multiple lines lhailey 2 20,386 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  Read serial BR=9600 . print me 2 lines? korenron 1 2,370 Dec-31-2019, 06:39 PM
Last Post: DeaD_EyE
  using vars from one file to match lines in another gt76_noobster 3 2,741 Jan-30-2019, 05:34 PM
Last Post: ichabod801
  Print multiple record lines ntigner 3 3,562 Feb-08-2018, 05:34 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020