+ 2

How is string slicing working in this code?

a = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line." start = 0 size = 1 def func(a): return a[start:start+size] iterator = iter(lambda: func(a), '\n') # Will generate values until '\n' for out in iterator: print(f"Iterator loaded {out}") start += size print("Encountered Newline!")

6th Oct 2022, 4:30 PM
Shantanu
Shantanu - avatar
23 Respuestas
+ 6
Maybe it will be easier for you to understand? text = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line." def func(arg): return arg # Will generate values until '\n' iterator = [] for i in text: if i != '\n': iterator += [func(i)] else: break for out in iterator: print(f"Iterator loaded {out}") print("Encountered Newline!") #👇 index = 0 func = lambda arg: arg[index] # Will generate values until '\n' iterator = [] for i in text: if i != '\n': iterator += [func(i)] else: break for out in iterator: print(f"Iterator loaded {out}") index += 1 print("Encountered Newline!") #👇 def read(arg): for i in arg: if i != '\n': print(f"Iterator loaded {i}") else: print("Encountered Newline!") break read(text) #👇 i = 0 func = lambda arg: arg[i] iterator = iter(lambda: func(text), '\n') for _ in iterator: print(next(iterator)) i += 1
6th Oct 2022, 6:46 PM
Solo
Solo - avatar
+ 2
Shantanu 👏👏👏👏👏 Bravo, now you can see what you have mastered: "lambda, iter, filter and regular expressions". Let's complicate the task a little more. 😎 Try writing a function that displays a line of the user's choice: "1,2, or 3". 👋😎 P. S: "Are you cheating? Removed the period at the end of the sentence from the text." 😉 import re a = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line." b = r"\n[A-Za-z0-9 ]{1,}.\n" c = re.search(b, a) z = "" if c: z = c.group() s = (filter(lambda x: x != "\n", z)) u = len(z) z = iter(s) for i in range(u-2): print(next(z)) 👇 import re a = "This is a long string consisting of two lines.\nThis is the second line\nThis is the third line." b = r"[A-Za-z0-9 ]{1,}\n" c = re.search(b, a) if c: z = c.group() u = len(z) z = iter(z) for i in range(u-1): print(next(z))
12th Oct 2022, 9:13 AM
Solo
Solo - avatar
+ 2
Shantanu Great, you've done a great job. But why are you repeating the same mistakes that I have already corrected for you? And why write the same condition several times? Is it not possible to prescribe everything at once in one?
14th Oct 2022, 11:42 AM
Solo
Solo - avatar
+ 2
Shantanu Look closely at my previous answer and compare it to your code.
15th Oct 2022, 10:00 AM
Solo
Solo - avatar
+ 2
Shantanu great, now much better 👏👏👏👍😎 You either use a regular expression or a filter. Correction: #Run the code and type 1 if you want to print the 1st line, 2 for the second line and 3 for the third line import re a = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line." b = r"^([\w ]{1,}.\n)([\w ]{1,}.\n)([\w ]{1,}.$)" e = int(input()) c = re.search(b, a) _1 = 1 if c: x,y,z = c.groups() if e == 1: s = x elif e == 2: s = y elif e == 3: s = z _1 = 0 else: print("Text consists of three lines") d = iter(s) for i in range(len(s)-_1): print(next(d)) Now put all of this into a function whose argument will accept user input. Good luck!👋😎
17th Oct 2022, 12:20 PM
Solo
Solo - avatar
+ 2
Shantanu You are welcome. Trick question: "What if the text is 100 lines long, would you write a regular expression pattern with 100 groups?" 😎
17th Oct 2022, 1:09 PM
Solo
Solo - avatar
+ 2
Shantanu I've edited the question.
17th Oct 2022, 1:23 PM
Solo
Solo - avatar
+ 2
Shantanu , in my opinion this is the easiest way: text = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line." def println(n): for i in text.split('\n')[n-1]: print(f"Iterator loaded {i}") println(int(input()or 1)) #Or With ITER function: def println(arg,n): arg = arg.split('\n') if n > len(arg): return print(f'⚠️There is no line {n} in the text.') i = 0 func = lambda arg: arg[n-1][i] iterator=iter(lambda:func(arg),'.') for _ in iterator: print(next(iterator)) i += 1 print('.') println(text,int(input()or 1)) #Or: def println(n): arrtext = text.split('\n') if n > len(arrtext): return print(f'⚠️There is no line {n} in the text.') textln = iter(arrtext) for _ in range(n-1): next(textln) for i in next(textln): print(f"Iterator loaded {i}") println(int(input()or 1))
25th Oct 2022, 11:38 PM
Solo
Solo - avatar
+ 1
To pin the material, write a program to read the second line. 😎
7th Oct 2022, 9:41 AM
Solo
Solo - avatar
+ 1
Yes 😎
7th Oct 2022, 10:33 AM
Solo
Solo - avatar
7th Oct 2022, 12:11 PM
Shantanu
Shantanu - avatar
+ 1
Shantanu , okay, but let's assume you don't know the length of the string if you let the text enter the user. How then? 😉 Try doing this using the iter() function according to the topic of this discussion. Good luck 👋😎
7th Oct 2022, 6:51 PM
Solo
Solo - avatar
12th Oct 2022, 3:08 AM
Shantanu
Shantanu - avatar
13th Oct 2022, 9:55 AM
Shantanu
Shantanu - avatar
+ 1
Solo thanks for correction.
17th Oct 2022, 12:51 PM
Shantanu
Shantanu - avatar
+ 1
Solo, Your code is great 😃👍.
26th Oct 2022, 8:06 AM
Shantanu
Shantanu - avatar
+ 1
Shantanu thanks 🖐️😎
26th Oct 2022, 8:52 AM
Solo
Solo - avatar
0
Solo is this a challenge for me?
7th Oct 2022, 10:22 AM
Shantanu
Shantanu - avatar
0
Yes
8th Oct 2022, 11:26 AM
Ilzat Yamaletdinov
Ilzat Yamaletdinov - avatar
0
Solo what mistake?
14th Oct 2022, 1:14 PM
Shantanu
Shantanu - avatar